Skip to content

Commit

Permalink
add strict parameter to inList() and multiple()
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeram committed May 7, 2012
1 parent 388053c commit fe0c7d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
4 changes: 4 additions & 0 deletions lib/Cake/Test/Case/Utility/ValidationTest.php
Expand Up @@ -1822,6 +1822,8 @@ public function testInList() {
$this->assertFalse(Validation::inList('three', array('one', 'two')));
$this->assertFalse(Validation::inList('1one', array(0, 1, 2, 3)));
$this->assertFalse(Validation::inList('one', array(0, 1, 2, 3)));
$this->assertFalse(Validation::inList('2', array(1, 2, 3)));
$this->assertTrue(Validation::inList('2', array(1, 2, 3), false));
}

/**
Expand Down Expand Up @@ -1932,6 +1934,8 @@ public function testMultiple() {
$this->assertFalse(Validation::multiple(array('foo', 'bar', 'baz', 'squirrel'), array('min' => 10)));

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

Expand Down
10 changes: 6 additions & 4 deletions lib/Cake/Utility/Validation.php
Expand Up @@ -527,9 +527,10 @@ public static function money($check, $symbolPosition = 'left') {
*
* @param mixed $check Value to check
* @param mixed $options Options for the check.
* @param boolean $strict Defaults to true, set to false to disable strict type check
* @return boolean Success
*/
public static function multiple($check, $options = array()) {
public static function multiple($check, $options = array(), $strict = true) {
$defaults = array('in' => null, 'max' => null, 'min' => null);
$options = array_merge($defaults, $options);
$check = array_filter((array)$check);
Expand All @@ -544,7 +545,7 @@ public static function multiple($check, $options = array()) {
}
if ($options['in'] && is_array($options['in'])) {
foreach ($check as $val) {
if (!in_array($val, $options['in'], true)) {
if (!in_array($val, $options['in'], $strict)) {
return false;
}
}
Expand Down Expand Up @@ -716,10 +717,11 @@ public static function url($check, $strict = false) {
*
* @param string $check Value to check
* @param array $list List to check against
* @param boolean $strict Defaults to true, set to false to disable strict type check
* @return boolean Success
*/
public static function inList($check, $list) {
return in_array($check, $list, true);
public static function inList($check, $list, $strict = true) {
return in_array($check, $list, $strict);
}

/**
Expand Down

0 comments on commit fe0c7d3

Please sign in to comment.