Commit 49f8d45d by Alexander Kochetov

ExistValidator final refactoring. `allowArray` checking also added to `validateValue` method.

parent 520f5fa1
...@@ -111,15 +111,13 @@ class ExistValidator extends Validator ...@@ -111,15 +111,13 @@ class ExistValidator extends Validator
$query = $this->createQuery($targetClass, $params); $query = $this->createQuery($targetClass, $params);
if (is_array($object->$attribute)) { if (is_array($object->$attribute)) {
if ($query->count("DISTINCT [[$targetAttribute]]") == count($object->$attribute)) { if ($query->count("DISTINCT [[$targetAttribute]]") != count($object->$attribute)) {
return; $this->addError($object, $attribute, $this->message);
}
} elseif ($query->exists()) {
return;
} }
} elseif (!$query->exists()) {
$this->addError($object, $attribute, $this->message); $this->addError($object, $attribute, $this->message);
} }
}
/** /**
* @inheritdoc * @inheritdoc
...@@ -136,6 +134,9 @@ class ExistValidator extends Validator ...@@ -136,6 +134,9 @@ class ExistValidator extends Validator
$query = $this->createQuery($this->targetClass, [$this->targetAttribute => $value]); $query = $this->createQuery($this->targetClass, [$this->targetAttribute => $value]);
if (is_array($value)) { if (is_array($value)) {
if (!$this->allowArray) {
return [$this->message, []];
}
return $query->count("DISTINCT [[$this->targetAttribute]]") == count($value) ? null : [$this->message, []]; return $query->count("DISTINCT [[$this->targetAttribute]]") == count($value) ? null : [$this->message, []];
} else { } else {
return $query->exists() ? null : [$this->message, []]; return $query->exists() ? 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