Skip to content

Commit

Permalink
Fix validation regarding multiple() and valid 0 value.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Scherer committed Jun 15, 2015
1 parent 464dc9c commit 30af859
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/Validation/Validation.php
Expand Up @@ -654,7 +654,9 @@ public static function multiple($check, array $options = [], $caseInsensitive =
$defaults = ['in' => null, 'max' => null, 'min' => null];
$options += $defaults;

$check = array_filter((array)$check);
$check = array_filter((array)$check, function ($value) {
return ($value || is_numeric($value));
});
if (empty($check)) {
return false;
}
Expand Down
12 changes: 7 additions & 5 deletions tests/TestCase/Validation/ValidationTest.php
Expand Up @@ -2141,17 +2141,18 @@ public function testMultiple()
$this->assertFalse(Validation::multiple(''));
$this->assertFalse(Validation::multiple(null));
$this->assertFalse(Validation::multiple([]));
$this->assertFalse(Validation::multiple([0]));
$this->assertFalse(Validation::multiple(['0']));
$this->assertTrue(Validation::multiple([0]));
$this->assertTrue(Validation::multiple(['0']));

$this->assertTrue(Validation::multiple([0, 3, 4, 5], ['in' => range(0, 10)]));
$this->assertFalse(Validation::multiple([0, 15, 20, 5], ['in' => range(0, 10)]));
$this->assertFalse(Validation::multiple([0, 5, 10, 11], ['in' => range(0, 10)]));
$this->assertFalse(Validation::multiple(['boo', 'foo', 'bar'], ['in' => ['foo', 'bar', 'baz']]));
$this->assertFalse(Validation::multiple(['foo', '1bar'], ['in' => range(0, 10)]));

$this->assertTrue(Validation::multiple([0, 5, 10, 11], ['max' => 3]));
$this->assertFalse(Validation::multiple([0, 5, 10, 11, 55], ['max' => 3]));
$this->assertFalse(Validation::multiple([1, 5, 10, 11], ['max' => 3]));
$this->assertTrue(Validation::multiple([0, 5, 10, 11], ['max' => 4]));
$this->assertFalse(Validation::multiple([0, 5, 10, 11, 55], ['max' => 4]));
$this->assertTrue(Validation::multiple(['foo', 'bar', 'baz'], ['max' => 3]));
$this->assertFalse(Validation::multiple(['foo', 'bar', 'baz', 'squirrel'], ['max' => 3]));

Expand All @@ -2166,7 +2167,8 @@ public function testMultiple()
$this->assertFalse(Validation::multiple([0, 5, 9, 8, 6, 2, 1], ['in' => range(0, 10), 'max' => 5]));
$this->assertFalse(Validation::multiple([0, 5, 9, 8, 11], ['in' => range(0, 10), 'max' => 5]));

$this->assertFalse(Validation::multiple([0, 5, 9], ['in' => range(0, 10), 'max' => 5, 'min' => 3]));
$this->assertTrue(Validation::multiple([0, 5, 9], ['in' => range(0, 10), 'max' => 5, 'min' => 3]));
$this->assertFalse(Validation::multiple(['', '5', '9'], ['max' => 5, 'min' => 3]));
$this->assertFalse(Validation::multiple([0, 5, 9, 8, 6, 2, 1], ['in' => range(0, 10), 'max' => 5, 'min' => 2]));
$this->assertFalse(Validation::multiple([0, 5, 9, 8, 11], ['in' => range(0, 10), 'max' => 5, 'min' => 2]));

Expand Down

0 comments on commit 30af859

Please sign in to comment.