default value in dropdownlist

Is there a simple way to create dropdownlist with default value (stored in model). Value should be selected when form appears firs time, if $model->attribute is filled then default value should not be selected.


Let’s say your model attribute has default value test.

Data parameter that you pass to CHtml.activeDropDownList() is an array that contains the same key test.

That way when your form is displayed for a first time (and until attribute value is default one), this option will be automatically selected.

Few things you need to ask yourself:

Are you always know default values?

What if one model attribute has default value test and another test123?

Maybe you have to look at prompt special HTML attribute of the fourth parameter of this method.

I think this special HTML attribute will fit your needs very well.

Find below example

//in form

<div class="row">

        <?php echo $form->labelEx($model,'node_types_id'); ?>

	<?php echo $form->dropDownList($model,'node_types_id', array(1=>'type_1', 2=>'type_2'));?>

	<?php echo $form->error($model,'node_types_id'); ?>


//in controller before you render the form set the default value

public function actionCreate()


	$model=new Nodes;		

	// Uncomment the following line if AJAX validation is needed

	// $this->performAjaxValidation($model);

	if (isset($_POST['Nodes']))







	//Setting the default value







As I need this dropdown in many places, I am looking for more centralized way then changing my controller (setting attribute value to default value).