Skip to content

Commit 8bf8556

Browse files
committed
[Validator] Fixed Choice when an empty array is used in the "choices" option
1 parent 5206603 commit 8bf8556

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)