yii: drop down list values and add empty selection

by prettyscripts on 2011-03-31 10:52

phpyii

values in drop down list called via CActiveForm::dropDownList() is be populated by passing array of value => text as parameter.

eg, to select sex of a person, in the view file for the form:

Code:

<?php echo $form->dropDownList($form, 'sex', array('1' => 'male', '2' => 'female')); ?>

too add text such as 'select one of the following' to the field, modification to array data is not required. simply add an attribute to htmlOptions parameter (see CHtml::listOptions() for futher detail):

Code:

<?php echo $form->dropDownList($form, 'sex',
    array('1' => 'male', '2' => 'female'),
    array('empty' => 'Select one of the following...')
); ?>

note line 3.