Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Case insensitive validation inList option. Resolves ticket 3521. Adde…

…d two tests to proof the faulty behavior of $strict here. Also address the faulty behavior in multiple() and replace it with case sensitivity.
  • Loading branch information...
commit f7e507145c5d1933959b9ed4270f975ef20d4326 1 parent a7a6fca
@dereuromark dereuromark authored
View
26 lib/Cake/Test/Case/Utility/ValidationTest.php
@@ -1945,8 +1945,15 @@ 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));
+ $this->assertTrue(Validation::inList('2', array(1, 2, 3)));
+ $this->assertFalse(Validation::inList('2x', array(1, 2, 3)));
+ $this->assertFalse(Validation::inList(2, array('1', '2x', '3')));
+ $this->assertFalse(Validation::inList('One', array('one', 'two')));
+
+ // case insensitive
+ $this->assertTrue(Validation::inList('one', array('One', 'Two'), true));
+ $this->assertTrue(Validation::inList('Two', array('one', 'two'), true));
+ $this->assertFalse(Validation::inList('three', array('one', 'two'), true));
}
/**
@@ -2065,14 +2072,24 @@ 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->assertTrue(Validation::multiple(array('0', '5', '9'), array('in' => range(0, 10), 'max' => 5)));
+
$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)));
$this->assertFalse(Validation::multiple(array(0, 5, 9), array('in' => range(0, 10), 'max' => 5, 'min' => 3)));
$this->assertFalse(Validation::multiple(array(0, 5, 9, 8, 6, 2, 1), array('in' => range(0, 10), 'max' => 5, 'min' => 2)));
$this->assertFalse(Validation::multiple(array(0, 5, 9, 8, 11), array('in' => range(0, 10), 'max' => 5, 'min' => 2)));
+
+ $this->assertFalse(Validation::multiple(array('2x', '3x'), array('in' => array(1, 2, 3, 4, 5))));
+ $this->assertFalse(Validation::multiple(array(2, 3), array('in' => array('1x', '2x', '3x', '4x'))));
+ $this->assertFalse(Validation::multiple(array('one'), array('in' => array('One', 'Two'))));
+ $this->assertFalse(Validation::multiple(array('Two'), array('in' => array('one', 'two'))));
+
+ // case insensitive
+ $this->assertTrue(Validation::multiple(array('one'), array('in' => array('One', 'Two')), true));
+ $this->assertTrue(Validation::multiple(array('Two'), array('in' => array('one', 'two')), true));
+ $this->assertFalse(Validation::multiple(array('three'), array('in' => array('one', 'two')), true));
}
/**
@@ -2333,6 +2350,7 @@ public function testMimeType() {
$this->assertTrue(Validation::mimeType($image, array('image/gif')));
$this->assertTrue(Validation::mimeType(array('tmp_name' => $image), array('image/gif')));
+ $this->assertFalse(Validation::mimeType($image, array('image/GIF')));
$this->assertFalse(Validation::mimeType($image, array('image/png')));
$this->assertFalse(Validation::mimeType(array('tmp_name' => $image), array('image/png')));
}
View
39 lib/Cake/Utility/Validation.php
@@ -540,7 +540,7 @@ public static function money($check, $symbolPosition = 'left') {
}
/**
- * Validate a multiple select.
+ * Validate a multiple select. Comparison is case sensitive by default.
*
* Valid Options
*
@@ -550,12 +550,13 @@ public static function money($check, $symbolPosition = 'left') {
*
* @param array $check Value to check
* @param array $options Options for the check.
- * @param boolean $strict Defaults to true, set to false to disable strict type check
+ * @param boolean $caseInsensitive Set to true for case insensitive comparison.
* @return boolean Success
*/
- public static function multiple($check, $options = array(), $strict = true) {
+ public static function multiple($check, $options = array(), $caseInsensitive = false) {
$defaults = array('in' => null, 'max' => null, 'min' => null);
$options = array_merge($defaults, $options);
+
$check = array_filter((array)$check);
if (empty($check)) {
return false;
@@ -567,8 +568,15 @@ public static function multiple($check, $options = array(), $strict = true) {
return false;
}
if ($options['in'] && is_array($options['in'])) {
+ if ($caseInsensitive) {
+ $options['in'] = array_map('mb_strtolower', $options['in']);
+ }
foreach ($check as $val) {
- if (!in_array($val, $options['in'], $strict)) {
+ $strict = !is_numeric($val);
+ if ($caseInsensitive) {
+ $val = mb_strtolower($val);
+ }
+ if (!in_array((string)$val, $options['in'], $strict)) {
return false;
}
}
@@ -766,15 +774,22 @@ public static function url($check, $strict = false) {
}
/**
- * Checks if a value is in a given list.
+ * Checks if a value is in a given list. Comparison is case sensitive by default.
*
- * @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
+ * @param string $check Value to check.
+ * @param array $list List to check against.
+ * @param boolean $caseInsensitive Set to true for case insensitive comparison.
+ * @return boolean Success.
*/
- public static function inList($check, $list, $strict = true) {
- return in_array($check, $list, $strict);
+ public static function inList($check, $list, $caseInsensitive = false) {
+ $strict = !is_numeric($check);
+
+ if ($caseInsensitive) {
+ $list = array_map('mb_strtolower', $list);
+ $check = mb_strtolower($check);
+ }
+
+ return in_array((string)$check, $list, $strict);
}
/**
@@ -896,7 +911,7 @@ public static function luhn($check, $deep = false) {
}
/**
- * Checks the mime type of a file
+ * Checks the mime type of a file. Comparison is case sensitive.
*
* @param string|array $check
* @param array $mimeTypes to check for
Please sign in to comment.
Something went wrong with that request. Please try again.