Skip to content

Commit

Permalink
Merge branch '2.6' into 2.7
Browse files Browse the repository at this point in the history
* 2.6:
  [Debug] Fix deprecated use of DebugClassLoader
  [Validator] Fixed Choice when an empty array is used in the "choices" option
  • Loading branch information
nicolas-grekas committed Apr 24, 2015
2 parents 13af66f + 02257f4 commit b774eb9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Constraints/ChoiceValidator.php
Expand Up @@ -37,7 +37,7 @@ public function validate($value, Constraint $constraint)
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
}

if (!$constraint->choices && !$constraint->callback) {
if (!is_array($constraint->choices) && !$constraint->callback) {
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
}

Expand Down
17 changes: 17 additions & 0 deletions Tests/Constraints/ChoiceValidatorTest.php
Expand Up @@ -150,6 +150,23 @@ public function testInvalidChoice()
->assertRaised();
}

public function testInvalidChoiceEmptyChoices()
{
$constraint = new Choice(array(
// May happen when the choices are provided dynamically, e.g. from
// the DB or the model
'choices' => array(),
'message' => 'myMessage',
));

$this->validator->validate('baz', $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"baz"')
->setCode(Choice::NO_SUCH_CHOICE_ERROR)
->assertRaised();
}

public function testInvalidChoiceMultiple()
{
$constraint = new Choice(array(
Expand Down

0 comments on commit b774eb9

Please sign in to comment.