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!)
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.
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:
<?php
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';
}