activeRadioButtonList - how to set the DEFAULT value


I have a form which uses 2 models (User,Role). So when I create a new user, I list all the available Roles and I’d like to assign one role to my new user.

How can I set a default value or set which one is CHECKED? (I’ll also need this when I update a user!)



Set the value in your model, something like:


class MyModel extends CActiveRecord {

  public $radioButtons = 1;



Same as for setting a default value for a text feild or select box.


it’s probably me, missing something :confused:

so I set a public variable $role_id - in my user model (since it’s not stored in the user table, I have pivot table for this).

When I hit the submit button on my form, in my $_POST[‘User’] variable I can see the correct role_id, assigned to my new user, however when I do the

$user -> attributes = $_POST[ 'User' ];

and var_dump the $user->attributes, it loses the ‘role_id’ (it has everything else).

So what am I missing?



is $role_id a safe attribute?

yes, it’s in the safeAttributes( return array( ‘role_id’ ) ).

it’s just so weird, I’ve done this before with other models and elements. Could it be specific to the RadioButtonList?


Check $_POST?

from CHtml::activeRadioButtonList() doc

Generates a radio button list for a model attribute. The model attribute value is used as the selection. If the attribute has input error, the input field’s CSS class will be appended with errorCss.

So after you create your model object, you would have to assign the db value to the attribute before you pass it to View.

[color="#000000"]S[/color][color="#8b0000"][color="#000000"]ome[/color][color="#000000"] mod that delete this post.

[/color][/color][color="#8b0000"][color="#000000"]I commented in the wrong topic. [/color][/color]

[color="#8b0000"][color="#000000"]Excuse me. [/color][b]


As krak3n has mentioned, to get a default selection, set a model property value to the desired default. For example, if your radioButtonList inside CActiveForm looked like so:


echo $form->radioButtonList($model, 'recoveryOption', array(

                                'password' => Yii::t('login', 'I forgot my password'),

                                'id' => Yii::t('login', 'I forgot my ID #'),

                                'both' => Yii::t('login', 'I forgot both'),



where ‘recoveryOption’ is an attribute of the LoginForm model and you wanted ‘password’ option to be selected by default, you could accomplish it like this:

class LoginForm extends CFormModel {

    public $recoveryOption = 'password';


Not sure if this answers your question or not.