activeDropDownList mark SELECTED

How do I mark one of the options in dropdown list as A SELECTED one?

Make $model->$attributeName the value that you want to select.

I am sorry. I was not quite specific. When I call activeDropDownList($model, $attribute, $data) in a view file (with $data like this: $data=array(value1=>display1, value2=>display2…) I would like, for example value2, to be selected automaticly in  dropdown list. like its done in html - <option SELECTED='SELECTED' value='value2'>display2</option>. The question is - how do I mark some option as SELECTED?

I have answered you. ;)

You just need to make sure the value of $model->$attribute is 'value2'.

i am sorry, but it only started to work when I made this - activeDropDownList($model, $attribute, $data, 'options'=>array($model->value2=>array('selected'=>true)));

In your model you would have the attribute name of your drop down list.

When you submit the form you use the $model -> attributes = $_POST['ModelName']; to set the attributes in your model.

Then when your form renders it will automatically select the right option from your drop down list. You don’t need to add any extra code into your array of list options, it is all done for you, it’s the best thing about frameworks, it saves time doing the repetitive things :)

Hope thats helped explain things.

Chris

Hi, I have the same problem and I still cannot let it work.

I have 2 models(tables), 'transaction' and 'type'. transaction.type_id is a foreign key for 'type'.

I want to build a activeDropDownList, it's ok, user can use the type name and the type_id will save to database. But I cannot assign the default value of it. The following is my code, could you give me some advise? thanks.

Please read krak3n's reply above.

Sorry, I red it, but still not work, I still don't understand it.

I use this code

But it generate the code

If I use activeTextField, it's perfect. But if I use activeDropDownList, it uses SELECTED, not value tag. I'm not very understand krak3n's reply, could you give me a hint or tell me where to find the document? thanks.

No, you don't need 'value' option. Simply set $transaction->type_id to be the value that you want to select by default.

Sorry, I tried the 2 codes but it still doesn't work.

(This is compile error)

You only need the following:

Double check the value of $transaction->type_id to make sure it contains the value you want to select initially.

I got it, it works, assume the default value is 3. Very appreciate ;D

You probably should set the default selection in your model class:

Otherwise, if a user makes a selection, the page will still display type 3 because of your assignment there.

Thanks, but the default value is not always the same, it depends on runtime. And I just tested 2 cases. (1) User saves with default selection (2) User changes to other value and save. The result is database is correct.

I use the default CRUD views and use above code in _form.php. And after save action, the "show" view is corrected value.

Does I miss anything ?

You just need to make sure your assignment occurs BEFORE you populate the model with user submitted data. Otherwise, the user selection gets lost.

I understand, because I assign the default in the view, it's ok. But maybe it's not a good idea to do it in the view layer. Because I'm just building a POC, it should be ok. I'll think about it, thanks.

Hey all!

I do this assignment ($model->attribute=$value) in the view layer too, before echo CHtml::activeDropDownList($model, $attribute, $data); Is that good idea to do it into the view layer?

I tried to do it in the controller, before rendering my form, but it doesn’t work.

It is certainly not a good decision to assign attributes in view files. It is better practice to do it in controller.

I think the reason why that didn’t work is you haven’t carefully passed local variables to views.

When controller action calls $this->render(), you have to specify an array as the second parameter with an item ‘model’=>$modelVarInController. Then you will be able to use $model in the corresponding views.