8

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 --']) ?>
Mojtaba
  • 4,852
  • 5
  • 21
  • 38

3 Answers3

14

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 --']) ?>
Mojtaba
  • 4,852
  • 5
  • 21
  • 38
10

'Selected' must be written with a capital letter 'S':

'options'=>['72'=>['Selected'=>true]]
TayTay
  • 6,882
  • 4
  • 44
  • 65
sylvain
  • 101
  • 1
  • 2
1

Just make sure that your model has the property cat_id set. Someplace in your controller just do a

$model->cat_id = filter_input_array(INPUT_GET, 'cat_id');

or

    $modelArray = filter_input_array(INPUT_GET, 'nameofmodel');
    $model->cat_id = $modelArray['cat_id'];

If you really want to do it like you did, probably you have to use the name of the model too in there.

    <?= $form->field($model, 'cat_id')->dropDownList(ArrayHelper::map(DeviceCats::find()->where(['is_deleted' => 'no'])->all(),'id','title'),['options' => [$_GET['SOMETHIGNHERE']['cat_id'] => ['selected'=>true]], 'prompt' => ' -- Select Category --']) ?>
Mihai P.
  • 9,307
  • 3
  • 38
  • 49
  • Thank you Mihai. Actually, I am sure about the existing the value in the array. Also, I have used var_dump($_GET) and it's on level 1. – Mojtaba Jun 30 '15 at 01:14
  • that is my point... it should not be. You are jumping through hoops to get that working and doing it wrong too. Instead you should move the code in the controller. Besides that you are using PHP globals directly and that is a no no. – Mihai P. Jun 30 '15 at 23:40