diff --git a/README.md b/README.md index 0a54a11..c1f60ee 100644 --- a/README.md +++ b/README.md @@ -75,7 +75,7 @@ Setting language and language dir globally: // boot or config file -use Valitron\Validator as V; +use Valitron\Validator as V; V::langDir(__DIR__.'/validator_lang'); // always set langDir before lang. V::lang('ar'); @@ -94,13 +94,15 @@ V::lang('ar'); * `integer` - Must be integer number * `length` - String must be certain length * `lengthBetween` - String must be between given lengths + * `lengthMin` - String must be greater than given length + * `lengthMax` - String must be less than given length * `min` - Minimum * `max` - Maximum * `in` - Performs in_array check on given array values * `notIn` - Negation of `in` rule (not in array of values) * `ip` - Valid IP address * `email` - Valid email address - * `url` - Valid URL + * `url` - Valid URL * `urlActive` - Valid URL with active DNS record * `alpha` - Alphabetic characters only * `alphaNum` - Alphabetic and numeric characters only diff --git a/lang/en.php b/lang/en.php index 0a83aeb..9335ce6 100644 --- a/lang/en.php +++ b/lang/en.php @@ -26,7 +26,9 @@ return array( 'contains' => "must contain %s", 'boolean' => "must be a boolean", 'lengthBetween' => "must be between %d and %d characters", - 'creditCard' => "must be a valid credit card number" + 'creditCard' => "must be a valid credit card number", + "lengthMin" => "must contain greater than %d characters", + "lengthMax" => "must contain less than %d characters" ); diff --git a/lang/ru.php b/lang/ru.php index 0a8e818..6aba11b 100644 --- a/lang/ru.php +++ b/lang/ru.php @@ -25,5 +25,8 @@ return array( 'dateAfter' => "должно содержать дату не ранее, чем %s", 'contains' => "должно содержать %s", 'boolean' => "должно содержать логическое значение", - 'lengthBetween' => "должно содержать от %d до %d символов" + 'creditCard' => "должно быть номером кредитной карты", + 'lengthBetween' => "должно содержать от %d до %d символов", + "lengthMin" => "должно содержать более %d символов", + "lengthMax" => "должно содержать менее %d символов" ); \ No newline at end of file diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index cfb2da1..2c8c87a 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -140,21 +140,6 @@ 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 * @@ -199,6 +184,49 @@ class Validator return $length == $params[0]; } + /** + * Validate the length of a string (between) + * + * @param string $field + * @param mixed $value + * @param array $params + * + * @return boolean + */ + protected function validateLengthBetween($field, $value, $params) + { + $length = $this->stringLength($value); + return $length >= $params[0] && $length <= $params[1]; + } + + /** + * Validate the length of a string (min) + * + * @param string $field + * @param mixed $value + * @param array $params + * + * @return boolean + */ + protected function validateLengthMin($field, $value, $params) + { + return $this->stringLength($value) >= $params[0]; + } + + /** + * Validate the length of a string (max) + * + * @param string $field + * @param mixed $value + * @param array $params + * + * @return boolean + */ + protected function validateLengthMax($field, $value, $params) + { + return $this->stringLength($value) <= $params[0]; + } + /** * Get the length of a string * @@ -693,7 +721,7 @@ class Validator /** * Determine whether a field is being validated by the given rule. - * + * * @param string $name The name of the rule * @param string $field The name of the field * @return boolean diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d1ed886..079d322 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -147,6 +147,34 @@ class ValidateTest extends BaseTestCase $this->assertFalse($v->validate()); } + public function testLengthMinValid() + { + $v = new Validator(array('str' => 'happy')); + $v->rule('lengthMin', 'str', 4); + $this->assertTrue($v->validate()); + } + + public function testLengthMinInvalid() + { + $v = new Validator(array('str' => 'sad')); + $v->rule('lengthMin', 'str', 4); + $this->assertFalse($v->validate()); + } + + public function testLengthMaxValid() + { + $v = new Validator(array('str' => 'sad')); + $v->rule('lengthMax', 'str', 4); + $this->assertTrue($v->validate()); + } + + public function testLengthMaxInvalid() + { + $v = new Validator(array('str' => 'sad')); + $v->rule('lengthMax', 'str', 2); + $this->assertFalse($v->validate()); + } + public function testMinValid() { $v = new Validator(array('num' => 5)); @@ -640,7 +668,7 @@ class ValidateTest extends BaseTestCase $v->rule('testRule', 'name', array('foo', 'bar'))->message('Invalid name selected.'); $this->assertFalse($v->validate()); } - + public function testBooleanValid() { $v = new Validator(array('test' => true));