Yii2 dropdownList select default option

Mojtaba picture Mojtaba · Jun 28, 2015 · Viewed 29.1k times · Source

I am returning cat_id value by GET in url to say that my dropdown list, which Item must be select. But it's not working.

<?= $form->field($model, 'cat_id')->dropDownList(
ArrayHelper::map(DeviceCats::find()
->where(['is_deleted' => 'no'])->all(),'id','title')
,['options' => [$_GET['cat_id'] => ['selected'=>true]]
, 'prompt' => ' -- Select Category --']) ?>

Answer

Mojtaba picture Mojtaba · Jun 30, 2015

Finally solved with an unbelievable change. Just changed the first letter of selected to capital ('selected' should be 'Selected'). Here is the code:

<?= $form->field($model, 'cat_id')->dropDownList(
ArrayHelper::map(DeviceCats::find()
->where(['is_deleted' => 'no'])->all(),'id','title')
,['options' => [$_GET['cat_id'] => ['Selected'=>'selected']]
, 'prompt' => ' -- Select Category --']) ?>