Answer the question
In order to leave comments, you need to log in
Login uniqueness when editing a user?
Hey!
I use Yii2 and write user management. With this function in the model I check the login for uniqueness
public function checkUniqueness($attribute, $params)
{
$data = self::find()->where(['alive'=>1, 'name'=>$this->login])->all();
if(!empty($data)) {
$this->addError($attribute, Yii::t('app', 'This login is occupied'));
}
}
['login', 'checkUniqueness', 'on' => self::SCENARIO_INSERT],
Answer the question
In order to leave comments, you need to log in
Примерно такая проверка для всех сценариев:
$collision = self::find()->where(['and', ['alive' => 1], ['name' => $this->login], ['!=', 'id', $this->id]])->count();
if ($collision > 0) {
...
}
для проверки уникальности если использовать валидатор свой
можно так
public function checkUniqueness($attribute, $params)
{
$data = self::find()->where([ 'name'=>$this->login])->one();
if(!is_null($data)){
$this->addError($attribute, Yii::t('app', 'This login is occupied'));
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question