SHOW:
|
|
- or go back to the newest paste.
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 | } |