After clicking the button to send the form, if some reuired fields are not filled in, the response will come about the fields that need to be filled. You can somehow show these errors, but only in a separate block, or for example flash -

foreach($model->errors as $error) Yii::$app->session->setFlash('error', $error);

How can this be done? Here is the controller code, ajax validation

  public function actionCreate() { $model = new ObjectForm(); $model->scenario = 'create'; if (Yii::$app->request->isAjax && $model->load(Yii::$app->request->post())) { Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); } if ($model->load(Yii::$app->request->post()) && $model->saveObject()) { return $this->redirect(['index']); } else { return $this->render('/objects/create', [ 'model' => $model, ]); } } 

enter image description here

    1 answer 1

    Well, the answer you have already formed, so you need to catch it at the front and show how you need and where you need.

    You can intercept everything and filter the desired

     $(document).ajaxComplete(function(event, jqxhr, settings) { console.log(settings.url, jqxhr.responseText); }); 

    Or try to display errors in the block you need.

     <?= $form->errorSummary($model); ?> 

    Or try to intercept events

     $('#object-form').on('afterValidate', function (event, messages, errorAttributes) { console.log(event, messages, errorAttributes); }).on('ajaxComplete', function (event, jqXHR, textStatus){ console.log(event, messages, errorAttributes); }); 

    It all depends on the specific implementation, and the number of forms you have on the page.