Since human stupidity is limitless, and my time for a diploma is not, I will create 501 topics about non-working ajax-validation.

There is a registration form:

<?php $form = ActiveForm::begin([ 'id' => 'reg-form', 'enableClientValidation' => true, 'enableAjaxValidation' => true ]) ?> //тут вот поля всяческие <?php ActiveForm::end() ?> 

There is a controller (it is not all, there is still the input form is processed first, if it matters - I will add):

 public function actionLogin() { if (!Yii::$app->user->isGuest) { return $this->goHome(); } $this->layout = 'alter'; //форма регистрации $modelSignUp = new SignUpForm(); $this->performAjaxValidation($modelSignUp); if (isset($_POST['SignUpForm'])) { //массово присваиваем элементы массива в переменные модели $modelSignUp->attributes = Yii::$app->request->post('SignUpForm'); if($modelSignUp->validate() && $modelSignUp->signup()){ return $this->goHome(); } return $this->render('login', [ 'modelLog' => $modelLog, 'modelSignUp' => $modelSignUp ]); } 

There is a function performAjaxValidation, written using Ctrl + C / Ctrl + V, and not using CRUD (IMHO, important):

  protected function performAjaxValidation($model) { // var_dump($_POST['ajax']); if(isset($_POST['ajax']) && $_POST['ajax']==='reg-form') { echo ActiveForm::validate($model); Yii::app()->end(); } } 

The meaning of the required validation is checking for the uniqueness of the postal address. There is a rule:

 ['email', 'unique', 'targetClass' => '\app\models\User', 'message' => 'Аккаунт с такой почтой уже зарегистрирован.'], 

And nothing works (so also the form is not sent, it is useless to press the button))). As soon as I remove, everything related to the Ajax - everything is sent, recorded, if the mail is repeated - it gives a validation error after the form is submitted. However, due to the layout features, I need an Ajax.

Where I nakosyachila?

Update: By the way, when you click on the button, you get an error 505. enter image description here

  • judging by the code above, you are using Yii2, but this is Yii::app()->end() from Yii 1, and will not work - Blacknife
  • @Blacknife In fact, thank you, I didn’t deal with Yii, I will understand) - DumbSailor

2 answers 2

Or, you can take performAjaxValidation into a separate action and add it to the ActiveForm config

 'validationUrl' => Url::to(['performAjaxValidation']), 

, or in actionLogin add something like:

 $modelSignUp = new SignUpForm(); if(\Yii::$app->request->isAjax && $modelSignUp->load(\Yii::$app->request->post())){ \Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($modelSignUp); } 

    I still wrap the form

      <?php Pjax::begin();?> <?php Pjax::end();?> 

    and in the handler, I do not add anything extra except isAjax (the example of a friend is higher), I simply process it as a normal form.