mirror of
https://github.com/vlucas/valitron.git
synced 2025-12-30 23:01:52 +00:00
Validation fixes
This commit is contained in:
parent
48dcdc7248
commit
c4be38862b
@ -146,8 +146,8 @@ V::lang('ar');
|
||||
* `dateBefore` - Field is a valid date and is before the given date
|
||||
* `dateAfter` - Field is a valid date and is after the given date
|
||||
* `contains` - Field is a string and contains the given string
|
||||
* `arrayContains` - Field is an array and contains the given array
|
||||
* `unique` - Field is an array and contains unique values
|
||||
* `arrayContains` - Field is an array or a scalar value and contains the given array
|
||||
* `containsUnique` - Field is an array and contains unique values
|
||||
* `creditCard` - Field is a valid credit card number
|
||||
* `instanceOf` - Field contains an instance of the given class
|
||||
* `optional` - Value does not need to be included in data array. If it is however, it must pass validation.
|
||||
|
||||
@ -30,5 +30,7 @@ return array(
|
||||
'creditCard' => "must be a valid credit card number",
|
||||
'lengthMin' => "must be at least %d characters long",
|
||||
'lengthMax' => "must not exceed %d characters",
|
||||
'instanceOf' => "must be an instance of '%s'"
|
||||
'instanceOf' => "must be an instance of '%s'",
|
||||
'containsUnique' => "must contain unique elements only",
|
||||
'arrayContains' => "contains invalid value",
|
||||
);
|
||||
|
||||
@ -30,5 +30,7 @@ return array(
|
||||
'creditCard' => "должно быть номером кредитной карты",
|
||||
'lengthMin' => "должно содержать более %d символов",
|
||||
'lengthMax' => "должно содержать менее %d символов",
|
||||
'instanceOf' => "должно быть объектом класса '%s'"
|
||||
'instanceOf' => "должно быть объектом класса '%s'",
|
||||
'containsUnique' => "должно содержать только уникальные элементы",
|
||||
'arrayContains' => "содержит неверное значение",
|
||||
);
|
||||
|
||||
@ -481,8 +481,11 @@ class Validator
|
||||
if (!isset($params[0])) {
|
||||
return false;
|
||||
}
|
||||
if (!is_array($value) || !is_array($params[0])) {
|
||||
return false;
|
||||
if (!is_array($params[0])) {
|
||||
$params[0] = array($params[0]);
|
||||
}
|
||||
if (is_scalar($value)) {
|
||||
return $this->validateIn($field, $value, $params);
|
||||
}
|
||||
|
||||
$intersect = array_intersect($value, $params[0]);
|
||||
@ -496,7 +499,7 @@ class Validator
|
||||
* @param array $value
|
||||
* @return bool
|
||||
*/
|
||||
protected function validateUnique($field, $value)
|
||||
protected function validateContainsUnique($field, $value)
|
||||
{
|
||||
if (!is_array($value)) {
|
||||
return false;
|
||||
|
||||
@ -884,6 +884,11 @@ class ValidateTest extends BaseTestCase
|
||||
$v = new Validator(array('test_field' => array(81, false, 'orange')));
|
||||
$v->rule('arrayContains', 'test_field', array(45, 'green', true, 'orange', null, 81, false));
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
// string value and validation target cast to array
|
||||
$v = new Validator(array('test_field' => 'blue'));
|
||||
$v->rule('arrayContains', 'test_field', 'blue');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testArrayContainsInvalid()
|
||||
@ -913,35 +918,35 @@ class ValidateTest extends BaseTestCase
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testUniqueValid()
|
||||
public function testContainsUniqueValid()
|
||||
{
|
||||
// numeric values
|
||||
$v = new Validator(array('test_field' => array(81, 3, 15)));
|
||||
$v->rule('unique', 'test_field');
|
||||
$v->rule('containsUnique', 'test_field');
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
// string values
|
||||
$v = new Validator(array('test_field' => array('white', 'green', 'blue')));
|
||||
$v->rule('unique', 'test_field');
|
||||
$v->rule('containsUnique', 'test_field');
|
||||
$this->assertTrue($v->validate());
|
||||
|
||||
// mixed values
|
||||
$v = new Validator(array('test_field' => array(81, false, 'orange')));
|
||||
$v->rule('unique', 'test_field');
|
||||
$v->rule('containsUnique', 'test_field');
|
||||
$this->assertTrue($v->validate());
|
||||
}
|
||||
|
||||
public function testUniqueInvalid()
|
||||
public function testContainsUniqueInvalid()
|
||||
{
|
||||
$v = new Validator(array('test_field' => array(81, false, 'orange', false)));
|
||||
$v->rule('unique', 'test_field');
|
||||
$v->rule('containsUnique', 'test_field');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
public function testUniqueInvalidValue()
|
||||
public function testContainsUniqueInvalidValue()
|
||||
{
|
||||
$v = new Validator(array('test_field' => 'lorem ipsum'));
|
||||
$v->rule('unique', 'test_field');
|
||||
$v->rule('containsUnique', 'test_field');
|
||||
$this->assertFalse($v->validate());
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user