bug #14448 [Validator] Fixed Choice when an empty array is used in the "choices" option (webmozart)
This PR was merged into the 2.6 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 | -
Commits
-------
a5a41b2
[Validator] Fixed Choice when an empty array is used in the "choices" option
This commit is contained in:
commit
d206ffd212
@ -36,7 +36,7 @@ class ChoiceValidator extends ConstraintValidator
|
|||||||
throw new UnexpectedTypeException($constraint, __NAMESPACE__.'\Choice');
|
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');
|
throw new ConstraintDefinitionException('Either "choices" or "callback" must be specified on constraint Choice');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -150,6 +150,23 @@ class ChoiceValidatorTest extends AbstractConstraintValidatorTest
|
|||||||
->assertRaised();
|
->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()
|
public function testInvalidChoiceMultiple()
|
||||||
{
|
{
|
||||||
$constraint = new Choice(array(
|
$constraint = new Choice(array(
|
||||||
|
Reference in New Issue
Block a user