DropDownList option tag problems.

Hello guys,

I’m trying to work with dropDownList from ActiveField…but i’m missing something with the “options Array”.

That’s my main code from _form.php file. What i wanna do is to make selected option tag based on array.




$servizi = Servizi::find()->all();

		$serviziLista = ArrayHelper::map($servizi,'idservizio','nome');

		$selected = [

			'1' => ['selected' => 'selected'],

			'2' => ['selected' => 'selected'],

			'3' => ['selected' => 'selected'],

		];







<?= $form->field($model, 'servizi_attivi')->dropDownList(

			['' => $serviziLista ],

			[	

				//'prompt'=> 'Please select',

				'options' => $selected,

				'multiple' => true,

				'size' => '5',

			]

		); 

	?>



but the output doesn’t care about my “option array”…none of my options tag is being selected.




<select size="5" multiple="" name="Clienti[servizi_attivi][]" class="form-control" id="clienti-servizi_attivi">

<optgroup label="">

<option value="1">Proxy/Firewall</option>

<option value="2">Voip</option>

<option value="3">Antivirus Centralizzato</option>

</optgroup>

</select>



I tried different way to test it like changing selected in true, i have read a lot of docs…dunno what to do now.

Thx for help anyway :)

I think that ‘selected’ attribute is handled from framework.

You have to set attribute as array, with selected id.




$servizi = Servizi::find()->all();

$serviziLista = ArrayHelper::map($servizi,'idservizio','nome');


$model->servizi_attivi = ['1','2','3'];


<?= $form->field($model, 'servizi_attivi')->dropDownList(

                        $serviziLista,

                        [       

                                'multiple' => true,

                                'size' => '5',

                        ]

                ); 

        ?>




Thanks a lot Fabrizio…that worked very well. :)