From 71dfa35a215a01b39bca646b2df473f62f5beb29 Mon Sep 17 00:00:00 2001 From: Nicolas PHILIPPE Date: Thu, 20 Dec 2018 18:23:49 +0100 Subject: [PATCH] add new `choices` wildcard in message --- .../Component/Validator/Constraints/ChoiceValidator.php | 2 ++ .../Validator/Tests/Constraints/ChoiceValidatorTest.php | 5 +++++ 2 files changed, 7 insertions(+) diff --git a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php index a2635309f0..c97d8a6880 100644 --- a/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php +++ b/src/Symfony/Component/Validator/Constraints/ChoiceValidator.php @@ -68,6 +68,7 @@ class ChoiceValidator extends ConstraintValidator if (!\in_array($_value, $choices, true)) { $this->context->buildViolation($constraint->multipleMessage) ->setParameter('{{ value }}', $this->formatValue($_value)) + ->setParameter('{{ choices }}', $this->formatValues($choices)) ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->setInvalidValue($_value) ->addViolation(); @@ -100,6 +101,7 @@ class ChoiceValidator extends ConstraintValidator } elseif (!\in_array($value, $choices, true)) { $this->context->buildViolation($constraint->message) ->setParameter('{{ value }}', $this->formatValue($value)) + ->setParameter('{{ choices }}', $this->formatValues($choices)) ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->addViolation(); } diff --git a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php index f9db12200e..0235408af0 100644 --- a/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php +++ b/src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php @@ -169,6 +169,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"baz"') + ->setParameter('{{ choices }}', '"foo", "bar"') ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); } @@ -186,6 +187,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"baz"') + ->setParameter('{{ choices }}', '') ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); } @@ -202,6 +204,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"baz"') + ->setParameter('{{ choices }}', '"foo", "bar"') ->setInvalidValue('baz') ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); @@ -275,6 +278,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"2"') + ->setParameter('{{ choices }}', '1, 2') ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised(); } @@ -291,6 +295,7 @@ class ChoiceValidatorTest extends ConstraintValidatorTestCase $this->buildViolation('myMessage') ->setParameter('{{ value }}', '"3"') + ->setParameter('{{ choices }}', '1, 2, 3') ->setInvalidValue('3') ->setCode(Choice::NO_SUCH_CHOICE_ERROR) ->assertRaised();