Use Model Attribute Default Value In Activeform / Activefield

Im trying to create a basic “Create New Model” Form in Yii2. I have an ActiveRecord Model named ‘Blogger’ with a status field, which is defined in the database table as “… TINYINT(1) NOT NULL DEFAULT 0”. I am using ActiveForm->field()->radioList() to display 2 radio buttons to represent 0 and 1.

The code for the field is as follows:




        <?= $form->field($blogger, 'status')->radioList(

            array(

                Blogger::STATUS_HIDDEN => 'Hidden',

                Blogger::STATUS_DISPLAYED => 'Displayed'

            )

        ); ?>



The resulting HTML looks as I expect, however, the default value of ‘0’ is not being checked/selected. When I print the new instance of this $blogger model, I see null values for ‘status’. I expect to see a 0.

How do I get the ActiveField to auto-check the radio button for ‘0’ when I create new records for this model?

Is there a function in the Blogger class file I need to use to set defaults? Is there anything I am missing with my calls to ActiveField->radioList() ?

There’s $model->loadDefaultValues() that loads default values into the model.

Excellent, that does exactly what I was hoping for. Thanks for the help!