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
@ -75,7 +75,7 @@ Setting language and language dir globally:
|
|||||||
|
|
||||||
// boot or config file
|
// 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::langDir(__DIR__.'/validator_lang'); // always set langDir before lang.
|
||||||
V::lang('ar');
|
V::lang('ar');
|
||||||
@ -94,13 +94,15 @@ 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
|
||||||
* `notIn` - Negation of `in` rule (not in array of values)
|
* `notIn` - Negation of `in` rule (not in array of values)
|
||||||
* `ip` - Valid IP address
|
* `ip` - Valid IP address
|
||||||
* `email` - Valid email address
|
* `email` - Valid email address
|
||||||
* `url` - Valid URL
|
* `url` - Valid URL
|
||||||
* `urlActive` - Valid URL with active DNS record
|
* `urlActive` - Valid URL with active DNS record
|
||||||
* `alpha` - Alphabetic characters only
|
* `alpha` - Alphabetic characters only
|
||||||
* `alphaNum` - Alphabetic and numeric characters only
|
* `alphaNum` - Alphabetic and numeric characters only
|
||||||
|
|||||||
@ -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"
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -25,5 +25,8 @@ return array(
|
|||||||
'dateAfter' => "должно содержать дату не ранее, чем %s",
|
'dateAfter' => "должно содержать дату не ранее, чем %s",
|
||||||
'contains' => "должно содержать %s",
|
'contains' => "должно содержать %s",
|
||||||
'boolean' => "должно содержать логическое значение",
|
'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);
|
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
|
||||||
*
|
*
|
||||||
@ -693,7 +721,7 @@ class Validator
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Determine whether a field is being validated by the given rule.
|
* Determine whether a field is being validated by the given rule.
|
||||||
*
|
*
|
||||||
* @param string $name The name of the rule
|
* @param string $name The name of the rule
|
||||||
* @param string $field The name of the field
|
* @param string $field The name of the field
|
||||||
* @return boolean
|
* @return boolean
|
||||||
|
|||||||
@ -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));
|
||||||
@ -640,7 +668,7 @@ class ValidateTest extends BaseTestCase
|
|||||||
$v->rule('testRule', 'name', array('foo', 'bar'))->message('Invalid name selected.');
|
$v->rule('testRule', 'name', array('foo', 'bar'))->message('Invalid name selected.');
|
||||||
$this->assertFalse($v->validate());
|
$this->assertFalse($v->validate());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testBooleanValid()
|
public function testBooleanValid()
|
||||||
{
|
{
|
||||||
$v = new Validator(array('test' => true));
|
$v = new Validator(array('test' => true));
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user