There is an actionCreate , a separate link to ajax validation is registered in the form, i.e.

 <?php $form = ActiveForm::begin([ 'enableClientValidation' => false, 'enableAjaxValidation' => true, 'validationUrl' => '/item/validate', ]); ?> 

The insert script is registered in the actionCreate controller ItemController

 public function actionCreate() { $model = new Item([ 'scenario' => 'insert' ]); } 

And actionValidate

 public function actionValidate() { if ( Yii::$app->request->isAjax ) { $model = new Item(); // получить здесь сценарий или текущую модель if ( $model->load( Yii::$app->request->post() ) ) { if ( $errors = ActiveForm::validate( $model ) ) { Yii::$app->response->format = Response::FORMAT_JSON; return $errors; } else { return true; } } } } 

The question is how to make validation work according to the script, i.e. in this case insert ?

  • $ model-> setScenario? - ilyaplot
  • Not understood. The scenarios are different, and the action validation is one, and this is actually the problem. Those. scenarios can be different. - Guest
  • Then you need to somehow pass the script. Maybe just make a hidden input or something like that. - ilyaplot
  • Understood thanks. - Guest
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

1 answer 1

 use yii\widgets\ActiveForm; /** * @param integer $id * @return array */ public function actionAjaxValidate($id = null) { $model = $id ? $this->findModel($id) : new Item(['scenario' => 'insert']); $model->load(Yii::$app->request->post()); Yii::$app->response->format = Response::FORMAT_JSON; return ActiveForm::validate($model); }