Validation fixes

This commit is contained in:
misantron 2018-04-20 21:00:34 +03:00
parent 48dcdc7248
commit c4be38862b
5 changed files with 85 additions and 73 deletions

View File

@ -146,8 +146,8 @@ V::lang('ar');
* `dateBefore` - Field is a valid date and is before the given date * `dateBefore` - Field is a valid date and is before the given date
* `dateAfter` - Field is a valid date and is after 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 * `contains` - Field is a string and contains the given string
* `arrayContains` - Field is an array and contains the given array * `arrayContains` - Field is an array or a scalar value and contains the given array
* `unique` - Field is an array and contains unique values * `containsUnique` - Field is an array and contains unique values
* `creditCard` - Field is a valid credit card number * `creditCard` - Field is a valid credit card number
* `instanceOf` - Field contains an instance of the given class * `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. * `optional` - Value does not need to be included in data array. If it is however, it must pass validation.

View File

@ -30,5 +30,7 @@ return array(
'creditCard' => "must be a valid credit card number", 'creditCard' => "must be a valid credit card number",
'lengthMin' => "must be at least %d characters long", 'lengthMin' => "must be at least %d characters long",
'lengthMax' => "must not exceed %d characters", '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",
); );

View File

@ -30,5 +30,7 @@ return array(
'creditCard' => "должно быть номером кредитной карты", 'creditCard' => "должно быть номером кредитной карты",
'lengthMin' => "должно содержать более %d символов", 'lengthMin' => "должно содержать более %d символов",
'lengthMax' => "должно содержать менее %d символов", 'lengthMax' => "должно содержать менее %d символов",
'instanceOf' => "должно быть объектом класса '%s'" 'instanceOf' => "должно быть объектом класса '%s'",
'containsUnique' => "должно содержать только уникальные элементы",
'arrayContains' => "содержит неверное значение",
); );

View File

@ -481,8 +481,11 @@ class Validator
if (!isset($params[0])) { if (!isset($params[0])) {
return false; return false;
} }
if (!is_array($value) || !is_array($params[0])) { if (!is_array($params[0])) {
return false; $params[0] = array($params[0]);
}
if (is_scalar($value)) {
return $this->validateIn($field, $value, $params);
} }
$intersect = array_intersect($value, $params[0]); $intersect = array_intersect($value, $params[0]);
@ -496,7 +499,7 @@ class Validator
* @param array $value * @param array $value
* @return bool * @return bool
*/ */
protected function validateUnique($field, $value) protected function validateContainsUnique($field, $value)
{ {
if (!is_array($value)) { if (!is_array($value)) {
return false; return false;

View File

@ -884,6 +884,11 @@ class ValidateTest extends BaseTestCase
$v = new Validator(array('test_field' => array(81, false, 'orange'))); $v = new Validator(array('test_field' => array(81, false, 'orange')));
$v->rule('arrayContains', 'test_field', array(45, 'green', true, 'orange', null, 81, false)); $v->rule('arrayContains', 'test_field', array(45, 'green', true, 'orange', null, 81, false));
$this->assertTrue($v->validate()); $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() public function testArrayContainsInvalid()
@ -913,35 +918,35 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testUniqueValid() public function testContainsUniqueValid()
{ {
// numeric values // numeric values
$v = new Validator(array('test_field' => array(81, 3, 15))); $v = new Validator(array('test_field' => array(81, 3, 15)));
$v->rule('unique', 'test_field'); $v->rule('containsUnique', 'test_field');
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
// string values // string values
$v = new Validator(array('test_field' => array('white', 'green', 'blue'))); $v = new Validator(array('test_field' => array('white', 'green', 'blue')));
$v->rule('unique', 'test_field'); $v->rule('containsUnique', 'test_field');
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
// mixed values // mixed values
$v = new Validator(array('test_field' => array(81, false, 'orange'))); $v = new Validator(array('test_field' => array(81, false, 'orange')));
$v->rule('unique', 'test_field'); $v->rule('containsUnique', 'test_field');
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
} }
public function testUniqueInvalid() public function testContainsUniqueInvalid()
{ {
$v = new Validator(array('test_field' => array(81, false, 'orange', false))); $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()); $this->assertFalse($v->validate());
} }
public function testUniqueInvalidValue() public function testContainsUniqueInvalidValue()
{ {
$v = new Validator(array('test_field' => 'lorem ipsum')); $v = new Validator(array('test_field' => 'lorem ipsum'));
$v->rule('unique', 'test_field'); $v->rule('containsUnique', 'test_field');
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }