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.

    1 answer 1

    And you yourself have driven yourself into this trap. Do not use one action for 3 forms - this is obviously an error, unnecessary complexity, an unnecessary increase in code. We must strive to make the code as simple as possible, more transparent, more understandable, and not vice versa. And calls of the duplicated code can be carried out in separate methods.

     public function actionForm1() { list($form1, $form2, $form3) = $this->initializateForms(); $this->validateForm($form1); return $this->render('index', compact('form1', 'form2', 'form3')); } public function actionForm2() { list($form1, $form2, $form3) = $this->initializateForms(); $this->validateForm($form2); return $this->render('index', compact('form1', 'form2', 'form3')); } public function actionForm3() { list($form1, $form2, $form3) = $this->initializateForms(); $this->validateForm($form3); return $this->render('index', compact('form1', 'form2', 'form3')); } protected function initializateForms() { return [ new MyForm1(), new MyForm2(), new MyForm3(), ]; } protected function validateForm($form) { if ( $form->load( Yii::$app->request->post() ) ) { $form->saveData(); } } 

    PS: Yii perfectly knows how to work with forms via ajax out of the box, and in this case, I would implement validation, sending the forms via ajax.