Question

I've created a custom phone constraint following the steps in this recipe from the Symfony2 cookbook.

The constraint class:

namespace Foo\Bundle\StackBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;

/**
 * @Annotation
 */
class Phone extends Constraint
{
  public $message = 'The Phone contains an illegal character';
}

The validator class:

namespace Foo\Bundle\StackBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

    /**
     * @Annotation
     */
    class PhoneValidator extends ConstraintValidator
    {
      public function validate($value, Constraint $constraint)
      {
        $length = strlen($value);

        if (is_null($value)) {
          return;
        }

        if ( $length > 14 || ! preg_match("/\([1-9]{2}\) [0-9]{4}-[0-9]{4}/", $value)) {
          $this->context->addViolation($constraint->message, array(), $value);
        }
      }
    }

This validator works fine, however I would like to use the Regex string constraint provided by Symfony2.

I've tried to implement this in the constraint class:

namespace Foo\Bundle\StackBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @Annotation
 */
class Phone extends Constraint
{

  public $message = 'The Phone contains an illegal character';
  public static function loadValidatorMetadata(ClassMetadata $metadata)
  {
    $metadata->addPropertyConstraint('description', new Assert\Regex(array(
      'pattern' => '/\([1-9]{2}\) [0-9]{4}-[0-9]{4}/'
    )));
  }
}

But it gives me a fatal error asking me to implement the validate method:

Fatal error: Class Foo\Bundle\StackBundle\Validator\Constraints\CepValidator contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Symfony\Component\Validator\ConstraintValidatorInterface::validate)

But the validate method is already implemented in the ConstraintValidator class (although if properly implemented, I think the pattern indicated in loadValidatorMetadata should be enough).

Any suggestions of how can I achieve this?

UPDATE:

It seems that all was working properly, in order for the Regex constraint to work, after you set the pattern in the constraint class, the validate method can be declare empty in the validator class like this:

namespace Foo\Bundle\StackBundle\Validator\Constraints;

use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;

/**
 * @Annotation
 */
class PhoneValidator extends ConstraintValidator
{
  public function validate($value, Constraint $constraint)
  {   
  }
}
Was it helpful?

Solution

The error is thrown for CepValidator not for PhoneValidator.

You have another file ...

src/Foo/Bundle/StackBundle/Validator/Constraints/CepValidator.php

... that's missing the validate() method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top