Multiple models in form with drop down

Greetings forum,

I am in need of help once more.

I have a form for a father model that can add child model forms on the fly based on an index. You can find this approach around the net for both yii2 and yii.

So I have something like:


<?= Html::activeInput('text', $model, '[' . $index . ']attribute'); ?>

in my child form, which works fine, once I submit it, it returns a nice array of model data.

The problem arises when I switched to a drop down list for one of my attributes and set it like :


<?= Html::activeDropDownList( $model, "[$index]attribute",['op1', 'op2']); ?>

which does NOT return in my array (or anywhere, for that matter).

Does anyone know why this is?

Figured it out,

all of your dropdowns need to have their $items in the


['value1'=>"label1", "value2"=>"label2"]

format AND their name and id need to look like this in source: [html]<id="modelname-0-attribute" name="ModelName[0][attribute]">[/html].

The "0" there is the $index value.

So in your code you need


Html::activeDropDownList($model, "[$index]attribute",['value1'=>'label1','value2'=>'label2','value3'=>'label3']);

for your attribute drop downs and


Html::dropDownList("ModelName[$index][other]", 'val1',['val1'=>'label1','val2'=>'label2'],['id'=>"modelname-$index-other"]);

for your miscellaneous drop downs.

If you get that wrong, your other drop downs(or maybe just the ones before it) won’t send data upon submit.