Skip to content

Commit

Permalink
bug #14463 [Validator] Fixed Choice when an empty array is used in th…
Browse files Browse the repository at this point in the history
…e "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
  • Loading branch information
fabpot committed Apr 27, 2015
2 parents 8f7a558 + 8bf8556 commit 01b2d8e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
Expand Up @@ -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');
}

Expand Down
Expand Up @@ -143,6 +143,20 @@ public function testInvalidChoice()
->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(
Expand Down

0 comments on commit 01b2d8e

Please sign in to comment.