8000 feature #29658 [Validator] Choices constraint improvement (nikophil) · symfony/symfony@c7d6bda · GitHub
[go: up one dir, main page]

Skip to content

Commit c7d6bda

Browse files
committed
feature #29658 [Validator] Choices constraint improvement (nikophil)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Validator] Choices constraint improvement | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | License | MIT Hi, here is a little improvement for the choice constraint: expose a new `choices` wildcard for the messages, in order to provide a way to display easily the valid choices to the user. thanks. Commits ------- 71dfa35 add new `choices` wildcard in message
2 parents 23cdc73 + 71dfa35 commit c7d6bda

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

src/Symfony/Component/Validator/Constraints/ChoiceValidator.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function validate($value, Constraint $constraint)
6868
if (!\in_array($_value, $choices, true)) {
6969
$this->context->buildViolation($constraint->multipleMessage)
7070
->setParameter('{{ value }}', $this->formatValue($_value))
71+
->setParameter('{{ choices }}', $this->formatValues($choices))
7172
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
7273
->setInvalidValue($_value)
7374
->addViolation();
@@ -100,6 +101,7 @@ public function validate($value, Constraint $constraint)
100101
} elseif (!\in_array($value, $choices, true)) {
101102
$this->context->buildViolation($constraint->message)
102103
->setParameter('{{ value }}', $this->formatValue($value))
104+
->setParameter('{{ choices }}', $this->formatValues($choices))
103105
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
104106
->addViolation();
105107
}

src/Symfony/Component/Validator/Tests/Constraints/ChoiceValidatorTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public function testInvalidChoice()
169169

170170
$this->buildViolation('myMessage')
171171
->setParameter('{{ value }}', '"baz"')
172+
->setParameter('{{ choices }}', '"foo", "bar"')
172173
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
173174
->assertRaised();
174175
}
@@ -186,6 +187,7 @@ public function testInvalidChoiceEmptyChoices()
186187

187188
$this->buildViolation('myMessage')
188189
->setParameter('{{ value }}', '"baz"')
190+
->setParameter('{{ choices }}', '')
189191
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
190192
->assertRaised();
191193
}
@@ -202,6 +204,7 @@ public function testInvalidChoiceMultiple()
202204

203205
$this->buildViolation('myMessage')
204206
->setParameter('{{ value }}', '"baz"')
207+
->setParameter('{{ choices }}', '"foo", "bar"')
205208
->setInvalidValue('baz')
206209
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
207210
->assertRaised();
@@ -275,6 +278,7 @@ public function testStrictDisallowsDifferentType()
275278

276279
$this->buildViolation('myMessage')
277280
->setParameter('{{ value }}', '"2"')
281+
->setParameter('{{ choices }}', '1, 2')
278282
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
279283
->assertRaised();
280284
}
@@ -291,6 +295,7 @@ public function testStrictWithMultipleChoices()
291295

292296
$this->buildViolation('myMessage')
293297
->setParameter('{{ value }}', '"3"')
298+
->setParameter('{{ choices }}', '1, 2, 3')
294299
->setInvalidValue('3')
295300
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
296301
->assertRaised();

0 commit comments

Comments
 (0)
0