Below is my code snippet of Yii framework 2.0 ActiveField/DropdownList
$form = ActiveForm::begin();
foreach ($settings as $index => $setting) {
for($i = 0; $i < 3; $i++) {
echo $form->field($setting, "[$index]option[]")->dropdownList(['0' => 'A', '1' => 'B', '2' => 'C']) ?>
}
}
ActiveForm::end();
With the for loop above I have a dynamic set of models within a form and each model has 3 dropdownLists of the same property option
which can be submitted as an array. When $i = 0
I would like to select option A
as default. When $i = 1
I would like to select option B
as default and When $i = 2
I would like to select option C
as default.
How can I do that?
Try:
for($i = 0; $i < 3; $i++)
{
$model->option[$i] = "$i";
echo $form->field($model, "option[$i]")
->dropdownList(['0' => 'A', '1' => 'B', '2' => 'C']);
}
echo $form->dropDownList(
$model,
'country_id',
Country::items(),
array(
'empty'=>'--Select a country--')
);