|
13 | 13 |
|
14 | 14 | use Symfony\Component\Validator\Constraint;
|
15 | 15 | use Symfony\Component\Validator\ConstraintValidator;
|
16 |
| -use Symfony\Component\Validator\Context\ExecutionContextInterface; |
17 | 16 | use Symfony\Component\Validator\Exception\ConstraintDefinitionException;
|
18 | 17 | use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
19 | 18 |
|
@@ -64,63 +63,38 @@ public function validate($value, Constraint $constraint)
|
64 | 63 | if ($constraint->multiple) {
|
65 | 64 | foreach ($value as $_value) {
|
66 | 65 | if (!in_array($_value, $choices, $constraint->strict)) {
|
67 |
| - if ($this->context instanceof ExecutionContextInterface) { |
68 |
| - $this->context->buildViolation($constraint->multipleMessage) |
69 |
| - ->setParameter('{{ value }}', $this->formatValue($_value)) |
70 |
| - ->addViolation(); |
71 |
| - } else { |
72 |
| - // 2.4 API |
73 |
| - $this->context->addViolation($constraint->multipleMessage, array( |
74 |
| - '{{ value }}' => $this->formatValue($_value), |
75 |
| - )); |
76 |
| - } |
| 66 | + $this->buildViolation($constraint->multipleMessage) |
| 67 | + ->setParameter('{{ value }}', $this->formatValue($_value)) |
| 68 | + ->setInvalidValue($_value) |
| 69 | + ->addViolation(); |
| 70 | + |
| 71 | + return; |
77 | 72 | }
|
78 | 73 | }
|
79 | 74 |
|
80 | 75 | $count = count($value);
|
81 | 76 |
|
82 | 77 | if ($constraint->min !== null && $count < $constraint->min) {
|
83 |
| - if ($this->context instanceof ExecutionContextInterface) { |
84 |
| - $this->context->buildViolation($constraint->minMessage) |
85 |
| - ->setParameter('{{ limit }}', $constraint->min) |
86 |
| - ->setPlural((int) $constraint->min) |
87 |
| - ->addViolation(); |
88 |
| - } else { |
89 |
| - // 2.4 API |
90 |
| - $this->context->addViolation($constraint->minMessage, array( |
91 |
| - '{{ limit }}' => $constraint->min, |
92 |
| - ), $value, (int) $constraint->min); |
93 |
| - } |
| 78 | + $this->buildViolation($constraint->minMessage) |
| 79 | + ->setParameter('{{ limit }}', $constraint->min) |
| 80 | + ->setPlural((int) $constraint->min) |
| 81 | + ->addViolation(); |
94 | 82 |
|
95 | 83 | return;
|
96 | 84 | }
|
97 | 85 |
|
98 | 86 | if ($constraint->max !== null && $count > $constraint->max) {
|
99 |
| - if ($this->context instanceof ExecutionContextInterface) { |
100 |
| - $this->context->buildViolation($constraint->maxMessage) |
101 |
| - ->setParameter('{{ limit }}', $constraint->max) |
102 |
| - ->setPlural((int) $constraint->max) |
103 |
| - ->addViolation(); |
104 |
| - } else { |
105 |
| - // 2.4 API |
106 |
| - $this->context->addViolation($constraint->maxMessage, array( |
107 |
| - '{{ limit }}' => $constraint->max, |
108 |
| - ), $value, (int) $constraint->max); |
109 |
| - } |
| 87 | + $this->buildViolation($constraint->maxMessage) |
| 88 | + ->setParameter('{{ limit }}', $constraint->max) |
| 89 | + ->setPlural((int) $constraint->max) |
| 90 | + ->addViolation(); |
110 | 91 |
|
111 | 92 | return;
|
112 | 93 | }
|
113 | 94 | } elseif (!in_array($value, $choices, $constraint->strict)) {
|
114 |
| - if ($this->context instanceof ExecutionContextInterface) { |
115 |
| - $this->context->buildViolation($constraint->message) |
116 |
| - ->setParameter('{{ value }}', $this->formatValue($value)) |
117 |
| - ->addViolation(); |
118 |
| - } else { |
119 |
| - // 2.4 API |
120 |
| - $this->context->addViolation($constraint->message, array( |
121 |
| - '{{ value }}' => $this->formatValue($value), |
122 |
| - )); |
123 |
| - } |
| 95 | + $this->buildViolation($constraint->message) |
| 96 | + ->setParameter('{{ value }}', $this->formatValue($value)) |
| 97 | + ->addViolation(); |
124 | 98 | }
|
125 | 99 | }
|
126 | 100 | }
|
0 commit comments