yii\validators\UniqueValidator::validateAttribute PHP Метод

validateAttribute() публичный Метод

public validateAttribute ( $model, $attribute )
    public function validateAttribute($model, $attribute)
    {
        /* @var $targetClass ActiveRecordInterface */
        $targetClass = $this->targetClass === null ? get_class($model) : $this->targetClass;
        $targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute;
        if (is_array($targetAttribute)) {
            $params = [];
            foreach ($targetAttribute as $k => $v) {
                $params[$v] = is_int($k) ? $model->{$v} : $model->{$k};
            }
        } else {
            $params = [$targetAttribute => $model->{$attribute}];
        }
        foreach ($params as $value) {
            if (is_array($value)) {
                $this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
                return;
            }
        }
        $query = $targetClass::find();
        $query->andWhere($params);
        if ($this->filter instanceof \Closure) {
            call_user_func($this->filter, $query);
        } elseif ($this->filter !== null) {
            $query->andWhere($this->filter);
        }
        if (!$model instanceof ActiveRecordInterface || $model->getIsNewRecord() || $model->className() !== $targetClass::className()) {
            // if current $model isn't in the database yet then it's OK just to call exists()
            // also there's no need to run check based on primary keys, when $targetClass is not the same as $model's class
            $exists = $query->exists();
        } else {
            // if current $model is in the database already we can't use exists()
            /* @var $models ActiveRecordInterface[] */
            $models = $query->select($targetClass::primaryKey())->limit(2)->all();
            $n = count($models);
            if ($n === 1) {
                $keys = array_keys($params);
                $pks = $targetClass::primaryKey();
                sort($keys);
                sort($pks);
                if ($keys === $pks) {
                    // primary key is modified and not unique
                    $exists = $model->getOldPrimaryKey() != $model->getPrimaryKey();
                } else {
                    // non-primary key, need to exclude the current record based on PK
                    $exists = reset($models)->getPrimaryKey() != $model->getOldPrimaryKey();
                }
            } else {
                $exists = $n > 1;
            }
        }
        if ($exists) {
            if (count($targetAttribute) > 1) {
                $this->addComboNotUniqueError($model, $attribute);
            } else {
                $this->addError($model, $attribute, $this->message);
            }
        }
    }

Usage Example

Пример #1
0
 public function uniqueCheck()
 {
     if ($this->owner instanceof ActiveRecordInterface) {
         /** @var Model $model */
         $model = clone $this->owner;
         $uniqueValidator = new UniqueValidator();
         $uniqueValidator->validateAttribute($model, $this->slugAttributeName);
         return !$model->hasErrors($this->slugAttributeName);
     }
     throw new Exception('Can\'t check if the slug is unique.');
 }
All Usage Examples Of yii\validators\UniqueValidator::validateAttribute