diff --git a/lib/validator/sfValidatorBoolean.class.php b/lib/validator/sfValidatorBoolean.class.php old mode 100644 new mode 100755 index 43e476ef4..b7d92b667 --- a/lib/validator/sfValidatorBoolean.class.php +++ b/lib/validator/sfValidatorBoolean.class.php @@ -45,12 +45,13 @@ protected function configure($options = array(), $messages = array()) */ protected function doClean($value) { - if (in_array($value, $this->getOption('true_values'))) + $checkValue = $value === 0 ? '0' : $value; + if (in_array($checkValue, $this->getOption('true_values'))) { return true; } - if (in_array($value, $this->getOption('false_values'))) + if (in_array($checkValue, $this->getOption('false_values'))) { return false; } diff --git a/test/unit/validator/sfValidatorBooleanTest.php b/test/unit/validator/sfValidatorBooleanTest.php old mode 100644 new mode 100755 index 944455bf9..981f814c2 --- a/test/unit/validator/sfValidatorBooleanTest.php +++ b/test/unit/validator/sfValidatorBooleanTest.php @@ -10,7 +10,7 @@ require_once(__DIR__.'/../../bootstrap/unit.php'); -$t = new lime_test(17); +$t = new lime_test(23); $v = new sfValidatorBoolean(); @@ -31,6 +31,22 @@ $t->is($v->clean($false_value), false, '->clean() returns false if the value is in the false_values option'); } +// other special test cases +$t->is($v->clean(0), false, '->clean() returns false if the value is 0'); +$t->is($v->clean(false), false, '->clean() returns false if the value is false'); +$t->is($v->clean(1), true, '->clean() returns true if the value is 1'); +$t->is($v->clean(true), true, '->clean() returns true if the value is true'); +$t->is($v->clean(''), false, '->clean() returns false if the value is empty string as empty_value is false by default'); + +class MyFalseClass +{ + public function __toString() + { + return 'false'; + } +} +$t->is($v->clean(new MyFalseClass()), false, '->clean() returns false if the value is false'); + // required is false by default $t->is($v->clean(null), false, '->clean() returns false if the value is null');