Merge pull request #53 from Kilte/feature-length

Add min/max-length validators
This commit is contained in:
Vance Lucas 2014-04-10 10:53:37 -05:00
commit 430a8324ec
5 changed files with 82 additions and 20 deletions

View File

@ -94,6 +94,8 @@ V::lang('ar');
* `integer` - Must be integer number * `integer` - Must be integer number
* `length` - String must be certain length * `length` - String must be certain length
* `lengthBetween` - String must be between given lengths * `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 * `min` - Minimum
* `max` - Maximum * `max` - Maximum
* `in` - Performs in_array check on given array values * `in` - Performs in_array check on given array values

View File

@ -26,7 +26,9 @@ return array(
'contains' => "must contain %s", 'contains' => "must contain %s",
'boolean' => "must be a boolean", 'boolean' => "must be a boolean",
'lengthBetween' => "must be between %d and %d characters", '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"
); );

View File

@ -27,4 +27,6 @@ return array(
'boolean' => "должно содержать логическое значение", 'boolean' => "должно содержать логическое значение",
'lengthBetween' => "должно содержать от %d до %d символов", 'lengthBetween' => "должно содержать от %d до %d символов",
'creditCard' => "должно быть номером кредитной карты", 'creditCard' => "должно быть номером кредитной карты",
"lengthMin" => "должно содержать более %d символов",
"lengthMax" => "должно содержать менее %d символов"
); );

View File

@ -140,21 +140,6 @@ class Validator
return $this->validateRequired($field, $value) && in_array($value, $acceptable, true); 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 * Validate that a field is numeric
* *
@ -199,6 +184,49 @@ class Validator
return $length == $params[0]; 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 * Get the length of a string
* *

View File

@ -147,6 +147,34 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $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() public function testMinValid()
{ {
$v = new Validator(array('num' => 5)); $v = new Validator(array('num' => 5));