Skip to content

Commit

Permalink
[Validator] Added strict option to ChoiceConstraint.
Browse files Browse the repository at this point in the history
  • Loading branch information
marc.weistroff committed Jul 7, 2011
1 parent 498f72c commit df57e0f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/Symfony/Component/Validator/Constraints/Choice.php
Expand Up @@ -19,6 +19,7 @@ class Choice extends Constraint
public $choices;
public $callback;
public $multiple = false;
public $strict = false;
public $min = null;
public $max = null;
public $message = 'The value you selected is not a valid choice';
Expand Down
Expand Up @@ -73,7 +73,7 @@ public function isValid($value, Constraint $constraint)

return false;
}
} elseif (!in_array($value, $choices, true)) {
} elseif (!in_array($value, $choices, $constraint->strict)) {
$this->setMessage($constraint->message, array('{{ value }}' => $value));

return false;
Expand Down
Expand Up @@ -182,4 +182,15 @@ public function testTooManyChoices()
'{{ limit }}' => 2,
));
}

public function testStrictIsFalse()
{
$constraint = new Choice(array(
'choices' => array(1, 2),
'strict' => false,
));

$this->assertTrue($this->validator->isValid('2', $constraint));
$this->assertTrue($this->validator->isValid(2, $constraint));
}
}

0 comments on commit df57e0f

Please sign in to comment.