Hi:
Let’s say I have a model “Client”, and a Client can have many Contacts. So we have 2 models, related.
I’m in the update method of the controller, when I can modify data:
$model = Client::findOne($id);
(this model already have relations, so I can access $model->contacts to get all the contacts of the client)
Now, we are in the view … I put some client fields …
<?php
echo $form->field($model, 'company')->textInput();
echo $form->field($model, 'address')->textInput();
echo $form->field($model, 'email')->textInput();
...
?>
And now I want to put all the contacts:
foreach ($model->contacts as $contact) {
echo $form->field($model, 'contacts.name')->textInput();
echo $form->field($model, 'contacts.email')->textInput();
...
}
This is what it’s not working for me … I don’t know how to create the ActiveFields for those related fields, and also how can I put a block of blank fields, so a user can create a new contact … then in the controller, how can I handle the old ones modifications and the new one ?
Importante note: it’s not creating a view from 2 separate models (as suggested here: https://yii2-framework.readthedocs.io/en/latest/guide/input-multiple-models/).
I’m referring to create one unique view with one unique ActiveForm (not a Gridview) of the main model and as many “blocks” of inputs with the related model records, in order to modify and create a new one (or even delete one).
A way to explain
FORM
input with model.company
input with model.email
…
Contacts
{loop}
input with model.contacts.name input with model.contacts.email …
input with model.contacts.name input with model.contacts.email …
line with blank inputs so user can create a new one
{/loop}
/FORM