A
A
Alexander Laikov2017-01-30 13:08:42
Yii
Alexander Laikov, 2017-01-30 13:08:42

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' => 'Введите проверочный код'],
    ];
  }

In the database there is already a line with the name 'Name', if I mean in the form line again 'Name', then the validation show that everything is good. But when I check the validation for the second time using the validate () method, it already shows that this name is taken. What is it connected with?
-----------------
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,
    ]);

The uniqueness check takes place after the button is pressed and after the page is refreshed
----------------------------
Changed in the controller:
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,
    ]);
  }

added to the input fields the problem remained the same validation does not perform the desired function --------------------- This error occurs when entering data in the field'enableAjaxValidation' => true,
740c792b8aee43808416d047e3df4716.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Maxim Fedorov, 2017-01-30
@Florens

Faced with the fact that yii2 does not check the uniqueness of the field using the AJAX method.

Doesn't validate because you don't have ajax validation. Read the documentation on how to implement it correctly

D
Dmitry, 2017-01-30
@slo_nik

Good afternoon.
Watch this video , it talks about ajax validation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question