Advertisement
Guest User

http://stackoverflow.com/questions/34253540/symfony-validato

a guest
Dec 13th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /**
  2.      * @Callback()
  3.      */
  4.     public function validatePostalCode(ExecutionContextInterface $context)
  5.     {
  6.         $constraint = new ZipCode([
  7.             'country' => $this->country,
  8.         ]);
  9.         $violations = $context->getValidator()->validate($this->postalCode, $constraint);
  10.  
  11.         foreach ($violations as $violation) {
  12.             /* @var $violation ConstraintViolationInterface */
  13.             $builder = $context->buildViolation($violation->getMessage(), $violation->getParameters());
  14.             $builder->atPath('postalCode')
  15.                 ->setCode($violation->getCode())
  16.                 ->setInvalidValue($violation->getInvalidValue());
  17.  
  18.             // getPlural is only in ConstraintViolation prior to 3.0
  19.             if (method_exists($violation, 'getPlural')) {
  20.                 $builder->setPlural($violation->getPlural());
  21.             }
  22.  
  23.             // getCause is only in ConstraintViolation, not in ConstraintViolationInterface even in v3.0
  24.             if (method_exists($violation, 'getCause')) {
  25.                 $builder->setCause($violation->getCause());
  26.             }
  27.  
  28.             $builder->addViolation();
  29.         }
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement