framework/validators/UniqueValidator.php
public function validateAttribute($model, $attribute)
    {
        /* @var $targetClass ActiveRecordInterface */
        $targetClass = $this->getTargetClass($model);
        $targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute;
        $rawConditions = $this->prepareConditions($targetAttribute, $model, $attribute);
        $conditions = [$this->targetAttributeJunction === 'or' ? 'or' : 'and'];

        foreach ($rawConditions as $key => $value) {
            if (is_array($value)) {
                $this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
                return;
            }
            $conditions[] = [$key => $value];
        }

        $db = $targetClass::getDb();

        $modelExists = false;

        if ($this->forceMasterDb && method_exists($db, 'useMaster')) {
            $db->useMaster(function () use ($targetClass, $conditions, $model, &$modelExists) {
                $modelExists = $this->modelExists($targetClass, $conditions, $model);
            });
        } else {
            $modelExists = $this->modelExists($targetClass, $conditions, $model);
        }

        if ($modelExists) {
            if (is_array($targetAttribute) && count($targetAttribute) > 1) {
                $this->addComboNotUniqueError($model, $attribute);
            } else {
                $this->addError($model, $attribute, $this->message);
            }
        }
    }
public function validateAttribute($model, $attribute)
    {
        /* @var $targetClass ActiveRecordInterface */
        $targetClass = $this->getTargetClass($model);
        $targetAttribute = $this->targetAttribute === null ? $attribute : $this->targetAttribute;
        $rawConditions = $this->prepareConditions($targetAttribute, $model, $attribute);
        $conditions = [$this->targetAttributeJunction === 'or' ? 'or' : 'and'];

        foreach ($rawConditions as $key => $value) {
            if (is_array($value)) {
                $this->addError($model, $attribute, Yii::t('yii', '{attribute} is invalid.'));
                return;
            }
            $conditions[] = [$key => $value];
        }

        $db = $targetClass::getDb();

        $modelExists = false;

        if ($this->forceMasterDb && method_exists($db, 'useMaster')) {
            $db->useMaster(function () use ($targetClass, $conditions, $model, &$modelExists) {
                $modelExists = $this->modelExists($targetClass, $conditions, $model);
            });
        } else {
            $modelExists = $this->modelExists($targetClass, $conditions, $model);
        }

        if ($modelExists) {
            if (is_array($targetAttribute) && count($targetAttribute) > 1) {
                $this->addComboNotUniqueError($model, $attribute);
            } else {
                $this->addError($model, $attribute, $this->message);
            }
        }
    }
public function validateAttribute ( $ model , $ attribute ) { /* @var $targetClass ActiveRecordInterface */ $ targetClass = $ this -> getTargetClass ( $ model ) ; $ targetAttribute = $ this -> targetAttribute === null ? $ attribute : $ this -> targetAttribute ; $ rawConditions = $ this -> prepareConditions ( $ targetAttribute , $ model , $ attribute ) ; $ conditions = [ $ this -> targetAttributeJunction === 'or' ? 'or' : 'and' ] ; foreach ( $ rawConditions as $ key => $ value ) { if ( is_array ( $ value ) ) { $ this -> addError ( $ model , $ attribute , Yii :: t ( 'yii' , '{attribute} is invalid.' ) ) ; return ; } $ conditions [ ] = [ $ key => $ value ] ; } $ db = $ targetClass :: getDb ( ) ; $ modelExists = false ; if ( $ this -> forceMasterDb && method_exists ( $ db , 'useMaster' ) ) { $ db -> useMaster ( function ( ) use ( $ targetClass , $ conditions , $ model , & $ modelExists ) { $ modelExists = $ this -> modelExists ( $ targetClass , $ conditions , $ model ) ; } ) ; } else { $ modelExists = $ this -> modelExists ( $ targetClass , $ conditions , $ model ) ; } if ( $ modelExists ) { if ( is_array ( $ targetAttribute ) && count ( $ targetAttribute ) > 1 ) { $ this -> addComboNotUniqueError ( $ model , $ attribute ) ; } else { $ this -> addError ( $ model , $ attribute , $ this -> message ) ; } } }
{@inheritdoc}
{