I created a option list with 2 options: Yes and No like below
<?php echo $form->radioButtonList($model,'active', array(1=>'Yes', 0=>'No'), array('separator'=>"" )); ?>
How can I set option 1 to be selected by default ?
You can check a radio button by default by adding the checked HTML attribute to the <input> element. You can disable a radio button by adding the disabled HTML attribute to both the <label> and the <input> .
Give people control and align with their expectations (Good): It is better to have a selected radio button by default, given that people cannot deselect and set the button back to its original state once one has been selected. A default selection sets the correct user expectation.
Here are three ways of doing it in code behind: RadioButtonList1. SelectedIndex = 1; RadioButtonList1. SelectedValue = "Two"; RadioButtonList1.
Just select the RadioButtons in the form designer and set the 'checked' property to true for the elements you want to be checked by default.
You have to set $model->active = 1
in your controller.
I prefer on view page, just before the form element. as
<?php $model->isNewRecord ? $model->active = 1: $model->active = $model->active ; ?>
This will take care of Update action also.
You can just also set a default value in the Model itself:
Here's a form where I use a radioButtonList for reportType and have one selected by default:
class FreeReportForm extends CFormModel
{
public $userId;
public $email;
public $callId;
public $reportType = 1;
public $companyNumber;
public $expiry;
...
}
You can set value 1 as default selected value without using $model also
<?php echo $form->radioButtonList($model,'1', array(1=>'Yes', 0=>'No'), array('separator'=>"" )); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With