8000 bug #14463 [Validator] Fixed Choice when an empty array is used in th… · symfony/symfony@01b2d8e · GitHub
[go: up one dir, main page]

Skip to content

Commit 01b2d8e

Browse files
committed
bug #14463 [Validator] Fixed Choice when an empty array is used in the "choices" option (webmozart)
This PR was merged into the 2.3 branch. Discussion ---------- [Validator] Fixed Choice when an empty array is used in the "choices" option | Q | A | ------------- | --- | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #13853 | License | MIT | Doc PR | - This is a backport of #14448 for the 2.3 branch. Commits ------- 8bf8556 [Validator] Fixed Choice when an empty array is used in the "choices" option
2 parents 8f7a558 + 8bf8556 commit 01b2d8e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class ChoiceValidator extends ConstraintValidator
3232
*/
3333
public function validate($value, Constraint $constraint)
3434
{
35-
if (!$constraint->choices && !$constraint->callback) {
35+
if (!is_array($constraint->choices) && !$constraint->callback) {
3636
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
3737
}
3838

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,20 @@ public function testInvalidChoice()
143143
->assertRaised();
144144
}
145145

146+
public function testInvalidChoiceEmptyChoices()
147+
{
148+
$constraint = new Choice(array(
149+
'choices' => array(),
150+
'message' => 'myMessage',
151+
));
152+
153+
$this->validator->validate('baz', $constraint);
154+
155+
$this->buildViolation('myMessage')
156+
->setParameter('{{ value }}', '"baz"')
157+
->assertRaised();
158+
}
159+
146160
public function testInvalidChoiceMultiple()
147161
{
148162
$constraint = new Choice(array(

0 commit comments

Comments
 (0)
0