Add type check for between comparison

Return 0 if $value is not numeric value
This commit is contained in:
USAMI Kenta 2015-11-20 16:57:15 +09:00
parent 28254e76c2
commit 96221ea4d3
2 changed files with 22 additions and 2 deletions

View File

@ -322,7 +322,9 @@ class Validator
*/
protected function validateMin($field, $value, $params)
{
if (function_exists('bccomp')) {
if (!is_numeric($value)) {
return false;
} elseif (function_exists('bccomp')) {
return !(bccomp($params[0], $value, 14) == 1);
} else {
return $params[0] <= $value;
@ -340,7 +342,9 @@ class Validator
*/
protected function validateMax($field, $value, $params)
{
if (function_exists('bccomp')) {
if (!is_numeric($value)) {
return false;
} elseif (function_exists('bccomp')) {
return !(bccomp($value, $params[0], 14) == 1);
} else {
return $params[0] >= $value;

View File

@ -222,6 +222,14 @@ class ValidateTest extends BaseTestCase
$v = new Validator(array('num' => 5));
$v->rule('min', 'num', 6);
$this->assertFalse($v->validate());
$v = new Validator(array('test' => array()));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
$v = new Validator(array('test' => new stdClass));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
}
public function testMinInvalidFloat()
@ -262,6 +270,14 @@ class ValidateTest extends BaseTestCase
$v = new Validator(array('num' => 5));
$v->rule('max', 'num', 4);
$this->assertFalse($v->validate());
$v = new Validator(array('test' => array()));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
$v = new Validator(array('test' => new stdClass));
$v->rule('min', 'test', 1);
$this->assertFalse($v->validate());
}
public function testMaxInvalidFloat()