CHtml - activeDropDownList default value


In HTML, you’d set the value in the

<option value="1" selected>1</options>

How can I set this nicely with Yii?



Ooops, my bad :confused: in the controller I forgot to set the attributes of the form ie:

$form->setAttributes( array('attr1'=> $_POST['attrib1']) );

now it works as it meant to be :)


Hi imehesz,

I’m trying to do exactly the same, but I cannot understand your solution completely.

Could you please post a more detailed explanation? More code to read would be bautiful :)

Thanks a lot.


Well, I don’t have the exact code in front of me, but my problem was, that I implemented a search form on my page, and when I submitted the form, it always forgot the values in the view … because I forgot to assign them in the controller… so your controller would look something like this:

class MyController extends CController


  public function actionList()


    $form = new searchFormModel();

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


      // for some reason I had to set the variables one by one,

      // so I know for a fact this works

      $form -> setAttributes( array( 'name' => $_POST['searchForm']['name']) );

      // i think this might work too

      $form->attributes = $_POST['searchForm'];

      // ... some logic here ... //


    $this -> render( 'list', array( 'searchForm' => $form ) );



NOTE: I wrote this from the top of my head, but it should do the trick …

hope it helps,