From db6d49baf63328b48ed71dd1bcfb76cc92bd1a59 Mon Sep 17 00:00:00 2001 From: Kilte Date: Tue, 7 Jan 2014 00:08:09 +0400 Subject: [PATCH] Fix #33 Validation for length - Message is incorrect for between cases --- src/Valitron/Validator.php | 15 +++++++++++++++ tests/Valitron/ValidateTest.php | 4 ++-- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index 82fd830..b8c4428 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -139,6 +139,21 @@ class Validator return $this->validateRequired($field, $value) && in_array($value, $acceptable, true); } + /** + * Validate the length of a string + * + * @param string $filed + * @param mixed $value + * @param array $params + * + * @return boolean + */ + protected function validateLengthBetween($filed, $value, $params) + { + $length = $this->stringLength($value); + return $length >= $params[0] && $length <= $params[1]; + } + /** * Validate that a field is numeric * diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index 34f8cca..dce9829 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -136,14 +136,14 @@ class ValidateTest extends BaseTestCase public function testLengthBetweenValid() { $v = new Validator(array('str' => 'happy')); - $v->rule('length', 'str', 2, 8); + $v->rule('lengthBetween', 'str', 2, 8); $this->assertTrue($v->validate()); } public function testLengthBetweenInvalid() { $v = new Validator(array('str' => 'sad')); - $v->rule('length', 'str', 4, 10); + $v->rule('lengthBetween', 'str', 4, 10); $this->assertFalse($v->validate()); }