Answer the question
In order to leave comments, you need to log in
How to check only the newest entry for uniqueness in Yii2?
Tell me you need to edit the available data, but filter it so that there are unique data or those that are there. Suggest a more elegant version.
public function rules() {
return [
// username and password are both required
[['login', 'email'], 'required', 'message' => 'Необходимо заполнить поле.'],
['email', 'email'],
['login', 'match', 'pattern' => '/^[a-z]\w*$/i', 'message' => 'Только латинский шрифт, цифры и подчеркивыние.'],
[['password', 'confirmPassword'], 'validationPassword'],
[['id', 'login', 'email'], 'validationLoginEmail'],
];
}
public function validationLoginEmail($attribute, $params) {
(int) $this->id;
$patern_user = User::findByLogin($this->login);
if ($patern_user != null and $this->id != $patern_user->id ) {
$this->addError('login', 'Такой логин уже существует.');
}
$patern_user = User::findByEmail($this->email);
if ( $patern_user != null and $this->id != $patern_user->id) {
$this->addError('email', 'Такой e-mail уже существует.');
}
}
Answer the question
In order to leave comments, you need to log in
Please note that yii has update and create scripts, they can be created and used in rules, for example:
There is also $model->isNewRecord returning true or false
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question