How to preselect/check a default radio button in yii2 RadioList()?

Insane Skull picture Insane Skull · Jul 11, 2015 · Viewed 25.5k times · Source

I want the radio button preselected in my form.

 <?= $form->field($model, 'config')->radioList(['1'=>'Automatic Entry',2=>'Manual Entry'])
     ->label('Barcode/Book No Generation'); ?>

Answer

tarleb picture tarleb · Jul 11, 2015

The preselected values are taken from $model->config. That means that you should set that attribute to the value that you want preselected :

$model->config = '1';
$form->field($model, 'config')->radioList([
    '1' => 'Automatic Entry',
    '2' => 'Manual Entry',
]);

The relevant doc for this is in the ActiveForm class.