Skip to content

Loading…

Fix #427 #737

Merged
merged 1 commit into from

2 participants

@jails
Union of RAD member

The main problem here concern the empty string '' which match indifferently 0 or '0' due to PHP's casting.
This PR take this specific case into account to avoid matching issue with this specific value.

However other casting issues are left to the user e.g.:

$this->assertTrue(Validator::isInList('0', null, array('list' => array(true, false)))); //Pass
$this->assertTrue(Validator::isInList('1', null, array('list' => array(true, false)))); //Pass
$this->assertTrue(Validator::isInList('Gwoo lent his boat in exchange of beer', null, array('list' => array(true, false)))); //Pass
@nateabele nateabele merged commit 62b8b7a into UnionOfRAD:dev

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 13, 2012
  1. @jails

    Fix #427

    jails committed
Showing with 24 additions and 1 deletion.
  1. +22 −0 tests/cases/util/ValidatorTest.php
  2. +2 −1 util/Validator.php
View
22 tests/cases/util/ValidatorTest.php
@@ -414,6 +414,28 @@ function testInList() {
$this->assertTrue(Validator::isInList('one', null, array('list' => array('one', 'two'))));
$this->assertTrue(Validator::isInList('two', null, array('list' => array('one', 'two'))));
$this->assertFalse(Validator::isInList('3', null, array('list' => array('one', 'two'))));
+
+ $this->assertFalse(Validator::isInList('', null, array('list' => array('0', '1'))));
+ $this->assertFalse(Validator::isInList(null, null, array('list' => array('0', '1'))));
+ $this->assertFalse(Validator::isInList(false, null, array('list' => array('0', '1'))));
+ $this->assertFalse(Validator::isInList(true, null, array('list' => array('0', '1'))));
+
+ $this->assertFalse(Validator::isInList('', null, array('list' => array(0, 1))));
+ $this->assertFalse(Validator::isInList(null, null, array('list' => array(0, 1))));
+ $this->assertFalse(Validator::isInList(false, null, array('list' => array(0, 1))));
+ $this->assertFalse(Validator::isInList(true, null, array('list' => array(0, 1))));
+ $this->assertTrue(Validator::isInList(0, null, array('list' => array(0, 1))));
+ $this->assertTrue(Validator::isInList(1, null, array('list' => array(0, 1))));
+ $this->assertFalse(Validator::isInList(2, null, array('list' => array(0, 1))));
+
+ $this->assertTrue(Validator::isInList(0, null, array('list' => array('0', '1'))));
+ $this->assertTrue(Validator::isInList('1', null, array('list' => array('0', '1'))));
+
+ $this->assertTrue(Validator::isInList(1, null, array('list' => array('0', '1'))));
+ $this->assertTrue(Validator::isInList('1', null, array('list' => array('0', '1'))));
+
+ $this->assertFalse(Validator::isInList(2, null, array('list' => array('0', '1'))));
+ $this->assertFalse(Validator::isInList('2', null, array('list' => array('0', '1'))));
}
View
3 util/Validator.php
@@ -273,7 +273,8 @@ public static function __init() {
},
'inList' => function($value, $format, $options) {
$options += array('list' => array());
- return in_array($value, $options['list']);
+ $strict = is_bool($value) || $value === '';
+ return in_array($value, $options['list'], $strict);
},
'lengthBetween' => function($value, $format, $options) {
$length = strlen($value);
Something went wrong with that request. Please try again.