Hello! I have a page with several ActivForm forms on it.
Please give advice on how best to obtain data from these forms in the controller.
If getting the melons will be executed through one action, then it will be necessary to determine through what form the data came (there will be a lot of if) that I would like to avoid. The action will turn out pretty "fat."
public function actionIndex() { $model1 = MyForm1(); $model2 = MyForm2(); $model3 = MyForm3(); $result = Yii::$app->request->post(); //Этого очень хотелось бы избежать if (isset($result['form_1'])) { ... } elseif (isset($result['form_2'])) { ... return $this->render('index', ['form1' => $form1, 'form2' => $form2, 'form3' => $form3]); } ... return $this->render('index', ['form1' => $form1, 'form2' => $form2, 'form3' => $form3]); } If for each form to cut your action, then the code will be almost completely duplicated:
public function actionForm1() { $model1 = MyForm1(); $model2 = MyForm2(); $model3 = MyForm3(); if ( $model1->load( Yii::$app->request->post() ) ) { $model1->saveData(); } return $this->render('index', ['form1' => $form1, 'form2' => $form2, 'form3' => $form3]); } public function actionForm2() { $model1 = MyForm1(); $model2 = MyForm2(); $model3 = MyForm3(); if ( $model2->load( Yii::$app->request->post() ) ) { $model2->saveData(); } return $this->render('index', ['form1' => $form1, 'form2' => $form2, 'form3' => $form3]); } public function actionForm3() { $model1 = MyForm1(); $model2 = MyForm2(); $model3 = MyForm3(); if ( $model3->load( Yii::$app->request->post() ) ) { $model3->saveData(); } return $this->render('index', ['form1' => $form1, 'form2' => $form2, 'form3' => $form3]); } There is also an idea to combine everything into one form, but then there will be a problem with validation, since There is a lot of unrelated data.