Updated min and max rules to use bccomp for number comparison.

This commit is contained in:
Justin Hook 2014-04-02 21:58:30 +01:00
parent 4fb6f478ef
commit 631a5e2138
2 changed files with 22 additions and 6 deletions

View File

@ -224,7 +224,7 @@ class Validator
*/ */
protected function validateMin($field, $value, $params) protected function validateMin($field, $value, $params)
{ {
return $value >= $params[0]; return !(bccomp($params[0], $value, 14) == 1);
} }
/** /**
@ -238,7 +238,7 @@ class Validator
*/ */
protected function validateMax($field, $value, $params) protected function validateMax($field, $value, $params)
{ {
return $value <= $params[0]; return !(bccomp($value, $params[0], 14) == 1);
} }
/** /**

View File

@ -152,13 +152,21 @@ class ValidateTest extends BaseTestCase
$v = new Validator(array('num' => 5)); $v = new Validator(array('num' => 5));
$v->rule('min', 'num', 2); $v->rule('min', 'num', 2);
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
$v = new Validator(array('num' => 5));
$v->rule('min', 'num', 5);
$this->assertTrue($v->validate());
} }
public function testMinValidDecimal() public function testMinValidFloat()
{ {
$v = new Validator(array('num' => 0.9)); $v = new Validator(array('num' => 0.9));
$v->rule('min', 'num', 0.5); $v->rule('min', 'num', 0.5);
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
$v = new Validator(array('num' => 1 - 0.81));
$v->rule('min', 'num', 0.19);
$this->assertTrue($v->validate());
} }
public function testMinInvalid() public function testMinInvalid()
@ -168,7 +176,7 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testMinInvalidDecimal() public function testMinInvalidFloat()
{ {
$v = new Validator(array('num' => 0.5)); $v = new Validator(array('num' => 0.5));
$v->rule('min', 'num', 0.9); $v->rule('min', 'num', 0.9);
@ -180,13 +188,21 @@ class ValidateTest extends BaseTestCase
$v = new Validator(array('num' => 5)); $v = new Validator(array('num' => 5));
$v->rule('max', 'num', 6); $v->rule('max', 'num', 6);
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
$v = new Validator(array('num' => 5));
$v->rule('max', 'num', 5);
$this->assertTrue($v->validate());
} }
public function testMaxValidDecimal() public function testMaxValidFloat()
{ {
$v = new Validator(array('num' => 0.4)); $v = new Validator(array('num' => 0.4));
$v->rule('max', 'num', 0.5); $v->rule('max', 'num', 0.5);
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
$v = new Validator(array('num' => 1 - 0.83));
$v->rule('max', 'num', 0.17);
$this->assertTrue($v->validate());
} }
public function testMaxInvalid() public function testMaxInvalid()
@ -196,7 +212,7 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testMaxInvalidDecimal() public function testMaxInvalidFloat()
{ {
$v = new Validator(array('num' => 0.9)); $v = new Validator(array('num' => 0.9));
$v->rule('max', 'num', 0.5); $v->rule('max', 'num', 0.5);