public function validateAttribute($model, $attribute) { $value = $model->{$attribute}; if (isset($model->id) || isset($model->item_id)) { /** @noinspection PhpUndefinedFieldInspection */ $modelId = isset($model->id) ? $model->id : $model->item_id; if (Subject::find()->where(['!=', 'id', $modelId])->andWhere(['name' => $value])->exists()) { $model->addError($attribute, $this->message); } } else { if (Subject::find()->where(['name' => $value])->exists()) { $model->addError($attribute, $this->message); } } }
/** * Creates data provider instance with search query applied * * @param $params * * @return ActiveDataProvider */ public function search($params) { $query = parent::find(); if (!isset($params['sort'])) { $query->orderBy(['name' => SORT_ASC]); } $dataProvider = new ActiveDataProvider(['query' => $query]); $this->load($params); if (!$this->validate()) { // uncomment the following line if you do not want to any records when validation fails // $query->where('0=1'); return $dataProvider; } $query->andFilterWhere(['like', 'name', $this->name]); $query->andFilterWhere(['like', 'email', $this->email]); $query->andFilterWhere(['like', 'phone', $this->phone]); return $dataProvider; }