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
This commit is contained in:
Fabien Potencier 2015-04-27 14:22:06 +02:00
commit 01b2d8ef96
2 changed files with 15 additions and 1 deletions

View File

@ -32,7 +32,7 @@ class ChoiceValidator extends ConstraintValidator
*/
public function validate($value, Constraint $constraint)
{
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');
}

View File

@ -143,6 +143,20 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest
->assertRaised();
}
public function testInvalidChoiceEmptyChoices()
{
$constraint = new Choice(array(
'choices' => array(),
'message' => 'myMessage',
));
$this->validator->validate('baz', $constraint);
$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"baz"')
->assertRaised();
}
public function testInvalidChoiceMultiple()
{
$constraint = new Choice(array(