Change validation rule to 'subset', document and translation fixes

This commit is contained in:
misantron 2018-09-23 00:53:19 +03:00
parent 873c996db0
commit ee2ed6c3b9
5 changed files with 23 additions and 19 deletions

View File

@ -149,7 +149,7 @@ 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 or a scalar value and contains the given array * `subset` - Field is an array or a scalar and all elements are contained in the given array
* `containsUnique` - 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

View File

@ -13,6 +13,8 @@ return array(
'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",
'ipv4' => "is not a valid IPv4 address",
'ipv6' => "is not a valid IPv6 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",
@ -32,5 +34,5 @@ return array(
'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", 'containsUnique' => "must contain unique elements only",
'arrayContains' => "contains invalid value", 'subset' => "contains an item that is not in the list",
); );

View File

@ -2,17 +2,19 @@
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 адресом",
'ipv4' => "не является валидным IPv4 адресом",
'ipv6' => "не является валидным IPv6 адресом",
'email' => "не является валидным email адресом", 'email' => "не является валидным email адресом",
'url' => "не является валидной ссылкой", 'url' => "не является валидной ссылкой",
'urlActive' => "содержит не активную ссылку", 'urlActive' => "содержит не активную ссылку",
@ -32,5 +34,5 @@ return array(
'lengthMax' => "должно содержать менее %d символов", 'lengthMax' => "должно содержать менее %d символов",
'instanceOf' => "должно быть объектом класса '%s'", 'instanceOf' => "должно быть объектом класса '%s'",
'containsUnique' => "должно содержать только уникальные элементы", 'containsUnique' => "должно содержать только уникальные элементы",
'arrayContains' => "содержит неверное значение", 'subset' => "содержит элемент, не указанный в списке",
); );

View File

@ -481,7 +481,7 @@ class Validator
* @param array $params * @param array $params
* @return bool * @return bool
*/ */
protected function validateArrayContains($field, $value, $params) protected function validateSubset($field, $value, $params)
{ {
if (!isset($params[0])) { if (!isset($params[0])) {
return false; return false;

View File

@ -941,53 +941,53 @@ class ValidateTest extends BaseTestCase
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testArrayContainsValid() public function testSubsetValid()
{ {
// 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('arrayContains', 'test_field', array(45, 15, 3, 7, 28, 81)); $v->rule('subset', 'test_field', array(45, 15, 3, 7, 28, 81));
$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('arrayContains', 'test_field', array('green', 'orange', 'blue', 'yellow', 'white', 'brown')); $v->rule('subset', 'test_field', array('green', 'orange', 'blue', 'yellow', 'white', 'brown'));
$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('arrayContains', 'test_field', array(45, 'green', true, 'orange', null, 81, false)); $v->rule('subset', '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 // string value and validation target cast to array
$v = new Validator(array('test_field' => 'blue')); $v = new Validator(array('test_field' => 'blue'));
$v->rule('arrayContains', 'test_field', 'blue'); $v->rule('subset', 'test_field', 'blue');
$this->assertTrue($v->validate()); $this->assertTrue($v->validate());
} }
public function testArrayContainsInvalid() public function testSubsetInvalid()
{ {
$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, false, 7)); $v->rule('subset', 'test_field', array(45, 'green', true, 'orange', null, false, 7));
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testArrayContainsInvalidValue() public function testSubsetInvalidValue()
{ {
$v = new Validator(array('test_field' => 'black 45')); $v = new Validator(array('test_field' => 'black 45'));
$v->rule('arrayContains', 'test_field', array('black', 45)); $v->rule('subset', 'test_field', array('black', 45));
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }
public function testArrayContainsInvalidRule() public function testSubsetInvalidRule()
{ {
// rule value has invalid type // rule value has invalid type
$v = new Validator(array('test_field' => array('black', 45))); $v = new Validator(array('test_field' => array('black', 45)));
$v->rule('arrayContains', 'test_field', 'black 45'); $v->rule('subset', 'test_field', 'black 45');
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
// rule value not specified // rule value not specified
$v = new Validator(array('test_field' => array('black', 45))); $v = new Validator(array('test_field' => array('black', 45)));
$v->rule('arrayContains', 'test_field'); $v->rule('subset', 'test_field');
$this->assertFalse($v->validate()); $this->assertFalse($v->validate());
} }