2 forms, 2 models - one action

Hi guys!

I have 2 forms, from 2 models in one action.

How do i get the correct post-values based on what form has been submitted?


$model->load(Yii::$app->request->post())

This captures all the post data. My model is named Article();


die(var_dump($_POST['Article'])));

returns the form input, but


$model->load(Yii::$app->request->post('Article'))

does not work. How is this done in Yii 2?

And a bonus question, how can i set up two submit buttons in one form to invoke to different responses to the submit?

Thanks in advance.

I had the same problem and wait for answer on http://www.yiiframework.com/doc-2.0/guide-input-multiple-models.htm

Finally, I build form on 1 model extends Model and operate using class yii\db\Command in controller before validate select and after validate update or insert depands on count row select result.

This sounds like you need to use a ViewModel: a model that inherits yii\base\Model and which includes properties for both database models. When you submit the single form, the ViewModel class copies the entire post data into itself and then on save(), it puts the correct data into the correct type of database model and saves them both.

As for your second question, it is possible to have multiple submit buttons in a form but if you use jQuery, you cannot usually tell from $_POST which was clicked (because submit is routed via jQuery). There is a whole discussion here which suggests some Javascript tricks like saving the value into a hidden field onclick(): http://stackoverflow.com/questions/12850041/how-can-i-detect-which-submit-button-was-pressed