Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing Issue #234 #235

Merged
merged 3 commits into from
May 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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')))
$stringValue = $value !== false ? (string) $value : '0';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just think that to string conversion trigger more problem than it solve (when the given value is an array).

We have a bug with 0 then only handle this case to be strict, and this minimum patch is enough to make it pass added tests, is'nt it ?

$checkValue = $value;

// Zero is equals to any string as it is the result of string casting `int`.
if (0 === $checkValue) {
    $checkValue = '0';
}

Full brain storming is following

No need to check whether the $value is not false as a string casting of false is an empty string.

>>> (string) false
=> ""

Moreover this condition add an hard-coded configuration. Think when a child class add there own configuration.


So after checking the latest Symfony version validator which have another context.

I suggest this following logic

if (true === $value) {
    $stringValue = 'true';
} elseif (false === $value) {
    $stringValue = 'false';
} else {
    $stringValue = (string) $value;
}

But we must be BC compatible so as Boolean values are an edge case then it is better to skip the casting to string.

if (in_array($stringValue, $this->getOption('true_values')))
{
return true;
}

if (in_array($value, $this->getOption('false_values')))
if (in_array($stringValue, $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