0

There are many suggestions to begin the controller code with this:

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

I have tried a few standard methods following validation such as this without success:

if($model->save()){

 return $this->redirect(['index']);
         }
       }

       return $this->renderAjax('create', [
         'model' => $model,
     ]);

My question is what code can be placed under the call to 'return ActiveForm::validate($model)' to successfully execute $model->save upon successful validation ?

MichaelMcD
  • 31
  • 5

2 Answers2

1

I found a working solution and here is the link:

yii2 form input ajax validation will not perfom

Please note that I had originally an ajax post script appended to the form which is not required for this to work, and so I removed it. It seems that enableAjaxvalidation sends the post as an AJAX request anyway on submitting the form.

Also, this must be in both the controller and the form page:

use yii\widgets\ActiveForm;

MichaelMcD
  • 31
  • 5
0
if($model->save(false)){

 return $this->redirect(['index']);
         }
       }

       return $this->renderAjax('create', [
         'model' => $model,
     ]);
Akash Shrimali
  • 63
  • 3
  • 14