Search code examples
phphtmlyii2active-form

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


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'); ?>

Solution

  • 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.