|
22 | 22 | use Symfony\Component\Validator\Constraints\GreaterThan;
|
23 | 23 | use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
|
24 | 24 | use Symfony\Component\Validator\Constraints\IdenticalTo;
|
| 25 | +use Symfony\Component\Validator\Constraints\IsNull; |
25 | 26 | use Symfony\Component\Validator\Constraints\Language;
|
26 | 27 | use Symfony\Component\Validator\Constraints\Length;
|
27 | 28 | use Symfony\Component\Validator\Constraints\LessThan;
|
|
37 | 38 | use Symfony\Component\Validator\Mapping\MetadataInterface;
|
38 | 39 | use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;
|
39 | 40 | use Symfony\Component\Validator\Validation;
|
| 41 | +use Symfony\Contracts\Translation\LocaleAwareInterface; |
| 42 | +use Symfony\Contracts\Translation\TranslatorInterface; |
| 43 | +use Symfony\Contracts\Translation\TranslatorTrait; |
40 | 44 |
|
41 | 45 | /**
|
42 | 46 | * @author Przemysław Bogusz <przemyslaw.bogusz@tubotax.pl>
|
@@ -258,6 +262,40 @@ public function testNestedConstraintsAreNotExecutedWhenGroupDoesNotMatch()
|
258 | 262 |
|
259 | 263 | $this->assertCount(1, $violations);
|
260 | 264 | }
|
| 265 | + |
| 266 | + public function testTranslatorIsCalledOnConstraintBaseMessageAndViolations() |
| 267 | + { |
| 268 | + $translator = new class() implements TranslatorInterface, LocaleAwareInterface { |
| 269 | + use TranslatorTrait; |
| 270 | + |
| 271 | + public function trans(?string $id, array $parameters = [], string $domain = null, string $locale = null): string |
| 272 | + { |
| 273 | + if ('This value should satisfy at least one of the following constraints:' === $id) { |
| 274 | + return 'Dummy translation:'; |
| 275 | + } |
| 276 | + |
| 277 | + if ('This value should be null.' === $id) { |
| 278 | + return 'Dummy violation.'; |
| 279 | + } |
| 280 | + |
| 281 | + return $id; |
| 282 | + } |
| 283 | + }; |
| 284 | + |
| 285 | + $validator = Validation::createValidatorBuilder() |
| 286 | + ->setTranslator($translator) |
| 287 | + ->getValidator() |
| 288 | + ; |
| 289 | + |
| 290 | + $violations = $validator->validate('Test', [ |
| 291 | + new AtLeastOneOf([ |
| 292 | + new IsNull(), |
| 293 | + ]), |
| 294 | + ]); |
| 295 | + |
| 296 | + $this->assertCount(1, $violations); |
| 297 | + $this->assertSame('Dummy translation: [1] Dummy violation.', $violations->get(0)->getMessage()); |
| 298 | + } |
261 | 299 | }
|
262 | 300 |
|
263 | 301 | class ExpressionConstraintNested
|
|
0 commit comments