Browse Source

`allowArray` introduced.

tags/2.0.0-rc
Alexander Kochetov 11 years ago
parent
commit
252795ec94
  1. 18
      framework/validators/ExistValidator.php

18
framework/validators/ExistValidator.php

@ -63,6 +63,11 @@ class ExistValidator extends Validator
public $filter; public $filter;
/** /**
* @var boolean whether to allow array type attribute.
*/
public $allowArray = false;
/**
* @inheritdoc * @inheritdoc
*/ */
public function init() public function init()
@ -81,6 +86,9 @@ class ExistValidator extends Validator
$targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute; $targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute;
if (is_array($targetAttribute)) { if (is_array($targetAttribute)) {
if ($this->allowArray) {
throw new InvalidConfigException('The "targetAttribute" property must be configured as a string.');
}
$params = []; $params = [];
foreach ($targetAttribute as $k => $v) { foreach ($targetAttribute as $k => $v) {
$params[$v] = is_integer($k) ? $object->$v : $object->$k; $params[$v] = is_integer($k) ? $object->$v : $object->$k;
@ -89,11 +97,19 @@ class ExistValidator extends Validator
$params = [$targetAttribute => $object->$attribute]; $params = [$targetAttribute => $object->$attribute];
} }
foreach ($params as $value) {
if (!$this->allowArray && is_array($value)) {
$this->addError($object, $attribute, Yii::t('yii', '{attribute} is invalid.'));
return;
}
}
$targetClass = $this->targetClass === null ? get_class($object) : $this->targetClass; $targetClass = $this->targetClass === null ? get_class($object) : $this->targetClass;
$query = $this->createQuery($targetClass, $params); $query = $this->createQuery($targetClass, $params);
if (is_array($object->$attribute)) { if (is_array($object->$attribute)) {
if ($query->count() >= count($object->$attribute)) { if ($query->count("DISTINCT [[$targetAttribute]]") === count($object->$attribute)) {
return; return;
} }
} elseif ($query->exists()) { } elseif ($query->exists()) {

Loading…
Cancel
Save