activeDropDownList and selected option

Hi all

I’m so noob: I’ve been trying for a [so very] long time to make an activeDropDownList work with an option selected, without any success.

I’d like to have selected the option with the id equal to the user id. It sounds so simple: I tried using the htmloptions array but not in the correct way:

$operatore = Operators::model()->findAll(); 

<?php echo CHtml::activeDropDownList($model, 'operator_id',CHtml::listData($operator, 'id', 'name')), array('option selected'=>Yii::app()->user->id)); ?>

as I managed only to have weird html code, like

<select option selected="2" name="Clients[operator_id]" id="Clients_operator_id">

<option value="1">Monja</option>

<option value="2">Other Operator</option>


that obviously can’t work.

I’d like something like

<select option name="Clients[operator_id]" id="Clients_operator_id">

<option value="1">Monja</option>

<option value="2" selected>Other Operator</option>

how can I do this?

thank you in advance :)


Are you sure you need activeDropDownList here? It’s only required if you want to render a input for a model attribute. The attribute value is used as selected value automatically then.

In your case dropDownList is probably what you want. You can specify the selected option there.

you’re perfectly right :) .

thanks a lot.

You can write this before activeDropDownList: $model->option_name = some_value;

For example:

$model->country_id = 68;

echo CHtml::activeDropDownList($model,‘country_id’, CHtml::listData(Countries::model()->getCountries(), ‘id’, ‘name’));

that’s it !


I should probably just read every post and save myself a lot of time :)



Hi all, I also have a problem, any help would be appreciated.

In table "Treatment" there are two columns: women and men.

Then I make an active drop down list:

<?=CHtml::activeDropDownList($bookingmodel,‘men’,CHtml::listData(Treatment::model()->findAll(), ‘men’, ‘men’ ), array(‘prompt’=>‘Men’)); ?>

the thing is that there are some rows in the column that has no data or NULL, and that makes the drop down list with an option "selected" at the end.

I don’t want that to happen because the prompt “Men” now doesn’t work properly.