diff --git a/README.md b/README.md index 1eb8745..c684bc5 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/lang/en.php b/lang/en.php index 10c944b..6b11ba6 100644 --- a/lang/en.php +++ b/lang/en.php @@ -1,34 +1,36 @@ "is required", - 'equals' => "must be the same as '%s'", - 'different' => "must be different than '%s'", - 'accepted' => "must be accepted", - 'numeric' => "must be numeric", - 'integer' => "must be an integer", - 'length' => "must be %d characters long", - 'min' => "must be at least %s", - 'max' => "must be no more than %s", - 'in' => "contains invalid value", - 'notIn' => "contains invalid value", - 'ip' => "is not a valid IP address", - 'email' => "is not a valid email address", - 'url' => "is not a valid URL", - 'urlActive' => "must be an active domain", - 'alpha' => "must contain only letters a-z", - '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", - 'regex' => "contains invalid characters", - 'date' => "is not a valid date", - 'dateFormat' => "must be date with format '%s'", - 'dateBefore' => "must be date before '%s'", - 'dateAfter' => "must be date after '%s'", - 'contains' => "must contain %s", - 'boolean' => "must be a boolean", - 'lengthBetween' => "must be between %d and %d characters", - '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'" + 'required' => "is required", + 'equals' => "must be the same as '%s'", + 'different' => "must be different than '%s'", + 'accepted' => "must be accepted", + 'numeric' => "must be numeric", + 'integer' => "must be an integer", + 'length' => "must be %d characters long", + 'min' => "must be at least %s", + 'max' => "must be no more than %s", + 'in' => "contains invalid value", + 'notIn' => "contains invalid value", + 'ip' => "is not a valid IP address", + 'email' => "is not a valid email address", + 'url' => "is not a valid URL", + 'urlActive' => "must be an active domain", + 'alpha' => "must contain only letters a-z", + '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", + 'regex' => "contains invalid characters", + 'date' => "is not a valid date", + 'dateFormat' => "must be date with format '%s'", + 'dateBefore' => "must be date before '%s'", + 'dateAfter' => "must be date after '%s'", + 'contains' => "must contain %s", + 'boolean' => "must be a boolean", + 'lengthBetween' => "must be between %d and %d characters", + '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'", + 'containsUnique' => "must contain unique elements only", + 'arrayContains' => "contains invalid value", ); diff --git a/lang/ru.php b/lang/ru.php index 37c914c..01cd448 100644 --- a/lang/ru.php +++ b/lang/ru.php @@ -1,34 +1,36 @@ "обязательно для заполнения", - 'equals' => "должно содержать '%s'", - 'different' => "должно отличаться от '%s'", - 'accepted' => "должно быть указано", - 'numeric' => "должно содержать числовое значение", - 'integer' => "должно быть числом", - 'length' => "должно быть длиннее, чем %d", - 'min' => "должно быть больше, чем %s", - 'max' => "должно быть меньше, чем %s", - 'in' => "содержит неверное значение", - 'notIn' => "содержит неверное значение", - 'ip' => "не является валидным IP адресом", - 'email' => "не является валидным email адресом", - 'url' => "не является валидной ссылкой", - 'urlActive' => "содержит не активную ссылку", - 'alpha' => "должно содержать только латинские символы", - 'alphaNum' => "должно содержать только латинские символы и/или цифры", - 'slug' => "должно содержать только латинские символы, цифры, тире и подчёркивания", - 'regex' => "содержит недопустимые символы", - 'date' => "не является датой", - 'dateFormat' => "должно содержать дату следующего формата: %s", - 'dateBefore' => "должно содержать дату не позднее, чем %s", - 'dateAfter' => "должно содержать дату не ранее, чем %s", - 'contains' => "должно содержать %s", - 'boolean' => "должно содержать логическое значение", - 'lengthBetween' => "должно содержать от %d до %d символов", - 'creditCard' => "должно быть номером кредитной карты", - 'lengthMin' => "должно содержать более %d символов", - 'lengthMax' => "должно содержать менее %d символов", - 'instanceOf' => "должно быть объектом класса '%s'" + 'required' => "обязательно для заполнения", + 'equals' => "должно содержать '%s'", + 'different' => "должно отличаться от '%s'", + 'accepted' => "должно быть указано", + 'numeric' => "должно содержать числовое значение", + 'integer' => "должно быть числом", + 'length' => "должно быть длиннее, чем %d", + 'min' => "должно быть больше, чем %s", + 'max' => "должно быть меньше, чем %s", + 'in' => "содержит неверное значение", + 'notIn' => "содержит неверное значение", + 'ip' => "не является валидным IP адресом", + 'email' => "не является валидным email адресом", + 'url' => "не является валидной ссылкой", + 'urlActive' => "содержит не активную ссылку", + 'alpha' => "должно содержать только латинские символы", + 'alphaNum' => "должно содержать только латинские символы и/или цифры", + 'slug' => "должно содержать только латинские символы, цифры, тире и подчёркивания", + 'regex' => "содержит недопустимые символы", + 'date' => "не является датой", + 'dateFormat' => "должно содержать дату следующего формата: %s", + 'dateBefore' => "должно содержать дату не позднее, чем %s", + 'dateAfter' => "должно содержать дату не ранее, чем %s", + 'contains' => "должно содержать %s", + 'boolean' => "должно содержать логическое значение", + 'lengthBetween' => "должно содержать от %d до %d символов", + 'creditCard' => "должно быть номером кредитной карты", + 'lengthMin' => "должно содержать более %d символов", + 'lengthMax' => "должно содержать менее %d символов", + 'instanceOf' => "должно быть объектом класса '%s'", + 'containsUnique' => "должно содержать только уникальные элементы", + 'arrayContains' => "содержит неверное значение", ); diff --git a/src/Valitron/Validator.php b/src/Valitron/Validator.php index c377b2f..79c4236 100644 --- a/src/Valitron/Validator.php +++ b/src/Valitron/Validator.php @@ -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; diff --git a/tests/Valitron/ValidateTest.php b/tests/Valitron/ValidateTest.php index d5d6f3b..469cef3 100644 --- a/tests/Valitron/ValidateTest.php +++ b/tests/Valitron/ValidateTest.php @@ -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()); }