Skip to content

Commit 6f521e9

Browse files
authored
Merge pull request #235 from pathumhdes/issue234
Ensure sfValidatorBoolean::clean() returns false when 0 (integer zero) is passed in (fixes #234 )
2 parents 9620a13 + a38381f commit 6f521e9

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/validator/sfValidatorBoolean.class.php

100644100755
+3-2
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,13 @@ protected function configure($options = array(), $messages = array())
4545
*/
4646
protected function doClean($value)
4747
{
48-
if (in_array($value, $this->getOption('true_values')))
48+
$checkValue = $value === 0 ? '0' : $value;
49+
if (in_array($checkValue, $this->getOption('true_values')))
4950
{
5051
return true;
5152
}
5253

53-
if (in_array($value, $this->getOption('false_values')))
54+
if (in_array($checkValue, $this->getOption('false_values')))
5455
{
5556
return false;
5657
}

test/unit/validator/sfValidatorBooleanTest.php

100644100755
+17-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
require_once(__DIR__.'/../../bootstrap/unit.php');
1212

13-
$t = new lime_test(17);
13+
$t = new lime_test(23);
1414

1515
$v = new sfValidatorBoolean();
1616

@@ -31,6 +31,22 @@
3131
$t->is($v->clean($false_value), false, '->clean() returns false if the value is in the false_values option');
3232
}
3333

34+
// other special test cases
35+
$t->is($v->clean(0), false, '->clean() returns false if the value is 0');
36+
$t->is($v->clean(false), false, '->clean() returns false if the value is false');
37+
$t->is($v->clean(1), true, '->clean() returns true if the value is 1');
38+
$t->is($v->clean(true), true, '->clean() returns true if the value is true');
39+
$t->is($v->clean(''), false, '->clean() returns false if the value is empty string as empty_value is false by default');
40+
41+
class MyFalseClass
42+
{
43+
public function __toString()
44+
{
45+
return 'false';
46+
}
47+
}
48+
$t->is($v->clean(new MyFalseClass()), false, '->clean() returns false if the value is false');
49+
3450
// required is false by default
3551
$t->is($v->clean(null), false, '->clean() returns false if the value is null');
3652

0 commit comments

Comments
 (0)