Skip to content

Commit

Permalink
[Validator] Fixed using the strict option in the choice validator.
Browse files Browse the repository at this point in the history
  • Loading branch information
yethee committed Jul 16, 2011
1 parent 760b11c commit 0f328d2
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public function isValid($value, Constraint $constraint)

if ($constraint->multiple) {
foreach ($value as $_value) {
if (!in_array($_value, $choices, true)) {
if (!in_array($_value, $choices, $constraint->strict)) {
$this->setMessage($constraint->multipleMessage, array('{{ value }}' => $_value));

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -193,4 +193,37 @@ public function testStrictIsFalse()
$this->assertTrue($this->validator->isValid('2', $constraint));
$this->assertTrue($this->validator->isValid(2, $constraint));
}

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

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

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

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

public function testStrictIsTrueWhenMultipleChoices()
{
$constraint = new Choice(array(
'choices' => array(1, 2, 3),
'multiple' => true,
'strict' => true
));

$this->assertFalse($this->validator->isValid(array(2, '3'), $constraint));
}
}

0 comments on commit 0f328d2

Please sign in to comment.