Yii - Add Another Attribute To Dropdownlist

I will to add another attribute to dropDownList.

I will a dropdown list like this with Yii dropDownList:

<select name="city" id="city">

    <option value="1" test="123">one</option>

    <option value="2" test="234">two</option>

    <option value="3" test="345">three</option>

    <option value="4" test="456">four</option>


I will add test attribute to option tags.

Default Yii dropDownList is:


echo CHtml::activeDropDownList('City', 'City', array(1 => 'one', 2 => 'two')); 


How I can do this?


echo CHtml::activeDropDownList($model, 'City', array(1 => 'one', 2 => 'two', 3 => 'three', 4 => 'four')); 


Does you understand what I ask dude?

I need to add test attribute for dropdown

Going over the code in CHtml I’d say it can’t be done. You can add any attribute to the Select, but the options are generated based on the list you supply and they’re converted to an option list somewhere at the end of CHtml::listOptions() and it creates a limited set of attributes on which you cannot add your own.

It can be done of course if you extends CHtml and overrule the listOptions() function if you realy need it.

thanks dude

My dear friend this is your answer. ;)

the dropdownlist have a parameter, named $htmlOptions which you can add the extra options to it, But those options will apply just to select tag.

Maybe you should implement that with CHtml::tag(), But the best way is you mentioned in stackoverflow.

Oke, nice, I can’t read code then… Did it work?

thanks my phpdeveloper friend in forum :D

the codes in stackoverflow works perfect :)

I set another attribute to all option tag with this method