Skip to content

Commit

Permalink
merged branch yethee/choice_validator (PR #1713)
Browse files Browse the repository at this point in the history
Commits
-------

0f328d2 [Validator] Fixed using the strict option in the choice validator.

Discussion
----------

[Validator] Fixed using the strict option in the choice validator.
  • Loading branch information
fabpot committed Jul 16, 2011
2 parents 760b11c + 0f328d2 commit e3a14c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
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
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 e3a14c8

Please sign in to comment.