mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Add min/max-length validators
This commit is contained in:
parent
e011ab81c4
commit
8871f81476
@ -94,6 +94,8 @@ 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
|
||||
|
||||
@ -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"
|
||||
);
|
||||
|
||||
|
||||
|
||||
@ -25,5 +25,8 @@ return array(
|
||||
'dateAfter' => "должно содержать дату не ранее, чем %s",
|
||||
'contains' => "должно содержать %s",
|
||||
'boolean' => "должно содержать логическое значение",
|
||||
'lengthBetween' => "должно содержать от %d до %d символов"
|
||||
'creditCard' => "должно быть номером кредитной карты",
|
||||
'lengthBetween' => "должно содержать от %d до %d символов",
|
||||
"lengthMin" => "должно содержать более %d символов",
|
||||
"lengthMax" => "должно содержать менее %d символов"
|
||||
);
|
||||
@ -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
|
||||
*
|
||||
|
||||
@ -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));
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user