Answer the question
In order to leave comments, you need to log in
Yii2 does not check for uniqueness of a field. How to fix?
Good afternoon.
Faced with the fact that yii2 does not check the uniqueness of the field using the AJAX method.
public function rules() {
return [
['name', 'required', 'message' => 'Введите имя пользователя'],
['name', 'unique', 'targetClass' => 'app\models\User', 'message' => 'Данное имя пользователя уже занято.'],
['email', 'required', 'message' => 'Введите почтовый адрес'],
['email', 'unique', 'targetClass' => 'app\models\User', 'message' => 'Данный почтовый адрес уже занят'],
['email', 'email', 'message' => 'Некорректный формат почты'],
['password', 'required', 'message' => 'Введите Ваш пароль'],
['password', 'string', 'length' => [6,18], 'message' => 'Пароль должен содержать от 6 до 18 символов'],
['verifyCode', 'captcha', 'message' => 'Введите проверочный код'],
];
}
public function actionSignup() {
$form = new SignupForm;
if($form->load(Yii::$app->request->post())) {
if($form->validate() && $form->signup()) {
return $this->goHome();
}
}
return $this->render('signup', [
'
form' => $form,
]);
public function actionSignup() {
$form = new SignupForm;
if(Yii::$app->request->isAjax && $form->load(Yii::$app->request->post())) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($form);
}
if($form->load(Yii::$app->request->post())) {
if($form->signup()) {
return $this->goHome();
}
}
return $this->render('signup', [
'form' => $form,
]);
}
'enableAjaxValidation' => true,
Answer the question
In order to leave comments, you need to log in
Faced with the fact that yii2 does not check the uniqueness of the field using the AJAX method.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question