From 4fb6f478ef05cb2cf76115e0c18260308b9a7ed7 Mon Sep 17 00:00:00 2001 From: Justin Hook Date: Tue, 1 Apr 2014 21:17:00 +0100 Subject: [PATCH] Fixed issue with Min/Max rules using decimals --- src/Valitron/Validator.php | 4 ++-- tests/Valitron/ValidateTest.php | 28 ++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index cfb2da1..6d0831c 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -224,7 +224,7 @@ class Validator */ protected function validateMin($field, $value, $params) { - return (int) $value >= $params[0]; + return $value >= $params[0]; } /** @@ -238,7 +238,7 @@ class Validator */ protected function validateMax($field, $value, $params) { - return (int) $value <= $params[0]; + return $value <= $params[0]; } /** diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d1ed886..9809f2d 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -154,6 +154,13 @@ class ValidateTest extends BaseTestCase $this->assertTrue($v->validate()); } + public function testMinValidDecimal() + { + $v = new Validator(array('num' => 0.9)); + $v->rule('min', 'num', 0.5); + $this->assertTrue($v->validate()); + } + public function testMinInvalid() { $v = new Validator(array('num' => 5)); @@ -161,6 +168,13 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testMinInvalidDecimal() + { + $v = new Validator(array('num' => 0.5)); + $v->rule('min', 'num', 0.9); + $this->assertFalse($v->validate()); + } + public function testMaxValid() { $v = new Validator(array('num' => 5)); @@ -168,6 +182,13 @@ class ValidateTest extends BaseTestCase $this->assertTrue($v->validate()); } + public function testMaxValidDecimal() + { + $v = new Validator(array('num' => 0.4)); + $v->rule('max', 'num', 0.5); + $this->assertTrue($v->validate()); + } + public function testMaxInvalid() { $v = new Validator(array('num' => 5)); @@ -175,6 +196,13 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testMaxInvalidDecimal() + { + $v = new Validator(array('num' => 0.9)); + $v->rule('max', 'num', 0.5); + $this->assertFalse($v->validate()); + } + public function testInValid() { $v = new Validator(array('color' => 'green'));