Merge pull request #40 from Kilte/feature-between

Validation for length - Message is incorrect for between cases
This commit is contained in:
Vance Lucas 2014-01-06 14:05:25 -08:00
commit 725418b23a
5 changed files with 22 additions and 4 deletions

View File

@ -90,7 +90,8 @@ V::lang('ar');
* `accepted` - Checkbox or Radio must be accepted (yes, on, 1, true)
* `numeric` - Must be numeric
* `integer` - Must be integer number
* `length` - String must be certain length or between given lengths
* `length` - String must be certain length
* `lengthBetween` - String must be between given lengths
* `min` - Minimum
* `max` - Maximum
* `in` - Performs in_array check on given array values

View File

@ -25,6 +25,7 @@ return array(
'dateAfter' => "must be date after '%s'",
'contains' => "must contain %s",
'boolean' => "must be a boolean",
'lengthBetween' => "must be between %d and %d characters"
);

View File

@ -19,10 +19,11 @@ return array(
'alphaNum' => "должно содержать только латинские символы и/или цифры",
'slug' => "должно содержать только латинские символы, цифры, тире и подчёркивания",
'regex' => "содержит недопустимые символы",
'date' => "не являтся датой",
'date' => "не является датой",
'dateFormat' => "должно содержать дату следующего формата: %s",
'dateBefore' => "должно содержать дату не позднее, чем %s",
'dateAfter' => "должно содержать дату не ранее, чем %s",
'contains' => "должно содержать %s",
'boolean' => "должно содержать логическое значение",
'lengthBetween' => "должно содержать от %d до %d символов"
);

View File

@ -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
*

View File

@ -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());
}