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

@ -1,34 +1,36 @@
<?php <?php
return array( return array(
'required' => "is required", 'required' => "is required",
'equals' => "must be the same as '%s'", 'equals' => "must be the same as '%s'",
'different' => "must be different than '%s'", 'different' => "must be different than '%s'",
'accepted' => "must be accepted", 'accepted' => "must be accepted",
'numeric' => "must be numeric", 'numeric' => "must be numeric",
'integer' => "must be an integer", 'integer' => "must be an integer",
'length' => "must be %d characters long", 'length' => "must be %d characters long",
'min' => "must be at least %s", 'min' => "must be at least %s",
'max' => "must be no more than %s", 'max' => "must be no more than %s",
'in' => "contains invalid value", 'in' => "contains invalid value",
'notIn' => "contains invalid value", 'notIn' => "contains invalid value",
'ip' => "is not a valid IP address", 'ip' => "is not a valid IP address",
'email' => "is not a valid email address", 'email' => "is not a valid email address",
'url' => "is not a valid URL", 'url' => "is not a valid URL",
'urlActive' => "must be an active domain", 'urlActive' => "must be an active domain",
'alpha' => "must contain only letters a-z", 'alpha' => "must contain only letters a-z",
'alphaNum' => "must contain only letters a-z and/or numbers 0-9", 'alphaNum' => "must contain only letters a-z and/or numbers 0-9",
'slug' => "must contain only letters a-z, numbers 0-9, dashes and underscores", 'slug' => "must contain only letters a-z, numbers 0-9, dashes and underscores",
'regex' => "contains invalid characters", 'regex' => "contains invalid characters",
'date' => "is not a valid date", 'date' => "is not a valid date",
'dateFormat' => "must be date with format '%s'", 'dateFormat' => "must be date with format '%s'",
'dateBefore' => "must be date before '%s'", 'dateBefore' => "must be date before '%s'",
'dateAfter' => "must be date after '%s'", 'dateAfter' => "must be date after '%s'",
'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 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

@ -1,34 +1,36 @@
<?php <?php
return array( return array(
'required' => "обязательно для заполнения", 'required' => "обязательно для заполнения",
'equals' => "должно содержать '%s'", 'equals' => "должно содержать '%s'",
'different' => "должно отличаться от '%s'", 'different' => "должно отличаться от '%s'",
'accepted' => "должно быть указано", 'accepted' => "должно быть указано",
'numeric' => "должно содержать числовое значение", 'numeric' => "должно содержать числовое значение",
'integer' => "должно быть числом", 'integer' => "должно быть числом",
'length' => "должно быть длиннее, чем %d", 'length' => "должно быть длиннее, чем %d",
'min' => "должно быть больше, чем %s", 'min' => "должно быть больше, чем %s",
'max' => "должно быть меньше, чем %s", 'max' => "должно быть меньше, чем %s",
'in' => "содержит неверное значение", 'in' => "содержит неверное значение",
'notIn' => "содержит неверное значение", 'notIn' => "содержит неверное значение",
'ip' => "не является валидным IP адресом", 'ip' => "не является валидным IP адресом",
'email' => "не является валидным email адресом", 'email' => "не является валидным email адресом",
'url' => "не является валидной ссылкой", 'url' => "не является валидной ссылкой",
'urlActive' => "содержит не активную ссылку", 'urlActive' => "содержит не активную ссылку",
'alpha' => "должно содержать только латинские символы", 'alpha' => "должно содержать только латинские символы",
'alphaNum' => "должно содержать только латинские символы и/или цифры", 'alphaNum' => "должно содержать только латинские символы и/или цифры",
'slug' => "должно содержать только латинские символы, цифры, тире и подчёркивания", 'slug' => "должно содержать только латинские символы, цифры, тире и подчёркивания",
'regex' => "содержит недопустимые символы", 'regex' => "содержит недопустимые символы",
'date' => "не является датой", 'date' => "не является датой",
'dateFormat' => "должно содержать дату следующего формата: %s", 'dateFormat' => "должно содержать дату следующего формата: %s",
'dateBefore' => "должно содержать дату не позднее, чем %s", 'dateBefore' => "должно содержать дату не позднее, чем %s",
'dateAfter' => "должно содержать дату не ранее, чем %s", 'dateAfter' => "должно содержать дату не ранее, чем %s",
'contains' => "должно содержать %s", 'contains' => "должно содержать %s",
'boolean' => "должно содержать логическое значение", 'boolean' => "должно содержать логическое значение",
'lengthBetween' => "должно содержать от %d до %d символов", 'lengthBetween' => "должно содержать от %d до %d символов",
'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());
} }