Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

radioButtonList checked by default

Tags:

yii

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 ?

like image 940
razorfish Avatar asked May 24 '12 09:05

razorfish


People also ask

How to checked radio button 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> .

Should radio button be selected by default?

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.

How to set default value for radio button list asp net?

Here are three ways of doing it in code behind: RadioButtonList1. SelectedIndex = 1; RadioButtonList1. SelectedValue = "Two"; RadioButtonList1.

How to set default selected radio button in c#?

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.


4 Answers

You have to set $model->active = 1 in your controller.

like image 189
Puigcerber Avatar answered Sep 28 '22 10:09

Puigcerber


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.

like image 21
Ali Avatar answered Sep 28 '22 10:09

Ali


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;

    ...
}
like image 45
Tom Busby Avatar answered Oct 01 '22 10:10

Tom Busby


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'=>"" )); ?>
like image 22
Kavya Avatar answered Oct 01 '22 10:10

Kavya