Yii2 preselect radiobutton value

Shaeldon picture Shaeldon · Apr 10, 2015 · Viewed 7.3k times · Source

I know I can use

 <?= $form  ->field($model, 'subject')
            ->textInput(array('value' => 'VALUE'))
            ->label('Titel'); ?>

to prefill a Textfield, but how can I do it for a radioList?

<?= $form  ->field($model, 'locations')
           ->radioList($regionen)
           ->label('Regionen');

I could use ->textInput again but this transforms the whole List into a single Textfield

Alternativly: Is there a better way to modify a database record? Currently I'm trying to set all values into a new form.

Answer

Vasilis Lourdas picture Vasilis Lourdas · Apr 10, 2015

Put the value that you want selected in the locations attribute of your $model and after rendering, it will be pre-selected in the radio list. That is:

$model->locations = ....

I assume that locations is a foreign key to some other table (or maybe a fixed list of strings).