Skip to content

Commit 5d17445

Browse files
committed
Redefining the fix to avoid the BC break
1 parent e79e664 commit 5d17445

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

lib/validator/sfValidatorBoolean.class.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ class sfValidatorBoolean extends sfValidatorBase
3333
*/
3434
protected function configure($options = array(), $messages = array())
3535
{
36-
$this->addOption('true_values', array('true', 't', 'yes', 'y', 'on', '1', 1, true));
37-
$this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0', 0, false));
36+
$this->addOption('true_values', array('true', 't', 'yes', 'y', 'on', '1'));
37+
$this->addOption('false_values', array('false', 'f', 'no', 'n', 'off', '0'));
3838

3939
$this->setOption('required', false);
4040
$this->setOption('empty_value', false);
@@ -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'), true))
48+
$stringValue = $value !== false ? (string) $value : '0';
49+
if (in_array($stringValue, $this->getOption('true_values')))
4950
{
5051
return true;
5152
}
5253

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

test/unit/validator/sfValidatorBooleanTest.php

Lines changed: 17 additions & 1 deletion
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(21);
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)