How to set the checked attribute for a radio button generated by ActiveForm::radio() :

 echo $form->field( $model, 'radioButton' )->radio( [ 'checked' => 'checked', // or "'checked' => true" don't work ] ); 

The documentation for the ActiveForm::radio() method says:

... ...


Maybe it stretches because of the incorrect use of this method by me, since I did not understand what the following means:

This method will generate the "checked" tag attribute value.

This method will generate the “checked” attribute according to the value of the model attribute.

    1 answer 1

    I answer immediately myself, as long as I wrote - I understood.


    The thing is in inattentive reading of what was said in the documentation ...

    This method will generate the “checked” attribute according to the value of the model attribute .

    That is, the checked attribute is set automatically in accordance with the value of the model attribute (if the value of the model attribute matches the value of the current radio button, then the checked attribute is set to it):

     // По умолчанию у невыбранной радиокнопки значение 0, // а у выбранной — 1. $model->radioButton = '1'; echo $form->field( $model, 'radioButton' )->radio(); 

    The same goes for radioList() :

     // ... The selection of the radio buttons is taken from the value of the model // attribute. $model->radioButton = 'second'; echo $form->field( $model, 'radioButton' ) ->radioList( [ // <value> => <label> 'first' => 'Первый', 'second' => 'Второй', // Selected item ] );