Skip to content

Commit

Permalink
Merge pull request #235 from pathumhdes/issue234
Browse files Browse the repository at this point in the history
Ensure sfValidatorBoolean::clean() returns false when 0 (integer zero) is passed in (fixes #234 )
  • Loading branch information
thePanz authored May 25, 2021
2 parents 9620a13 + a38381f commit 6f521e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
5 changes: 3 additions & 2 deletions lib/validator/sfValidatorBoolean.class.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
18 changes: 17 additions & 1 deletion test/unit/validator/sfValidatorBooleanTest.php
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

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

$t = new lime_test(17);
$t = new lime_test(23);

$v = new sfValidatorBoolean();

Expand All @@ -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');

Expand Down

0 comments on commit 6f521e9

Please sign in to comment.