Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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