Commit af09fe16 by Alexander Kochetov

Improve `in` validator to properly support array attributes.

parent 433d67c7
...@@ -35,6 +35,10 @@ class RangeValidator extends Validator ...@@ -35,6 +35,10 @@ class RangeValidator extends Validator
* the attribute value should NOT be among the list of values defined via [[range]]. * the attribute value should NOT be among the list of values defined via [[range]].
*/ */
public $not = false; public $not = false;
/**
* @var boolean whether to allow array type attribute.
*/
public $allowArray = false;
/** /**
* @inheritdoc * @inheritdoc
...@@ -55,8 +59,18 @@ class RangeValidator extends Validator ...@@ -55,8 +59,18 @@ class RangeValidator extends Validator
*/ */
protected function validateValue($value) protected function validateValue($value)
{ {
$valid = !$this->not && in_array($value, $this->range, $this->strict) if (!$this->allowArray && is_array($value)) {
|| $this->not && !in_array($value, $this->range, $this->strict); return [$this->message, []];
}
$valid = false;
foreach ((array)$value as $v) {
if (!($valid = !$this->not && in_array($v, $this->range, $this->strict)
|| $this->not && !in_array($v, $this->range, $this->strict))) {
break;
}
}
return $valid ? null : [$this->message, []]; return $valid ? null : [$this->message, []];
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment