Yii 1.1: using multiple radio button

11 followers

When we use multiple radio buttons with same name in Yii many people have the problem on getting the value of the selected radiobutton. The radioButtonList can be used for that, but if we need to use radioButton itself to place the button any where in a view, we need to include one more parameter in htmloption array().

'uncheckValue'=>null

Usage ΒΆ

echo CHtml::radioButton('btn', false, array(
    'value'=>'1',
    'name'=>'btnname',
    'uncheckValue'=>null
));
 
echo CHtml::radioButton('btn', false, array(
    'value'=>'2',
    'name'=>'btnname',
    'uncheckValue'=>null
)); 
 
 
//we can use it in activeRadioButton(), too
echo $form->radioButton($model, 'name', array(
    'value'=>1,
    'uncheckValue'=>null
));
 
echo $form->radioButton($model, 'name', array(
    'value'=>2,
    'uncheckValue'=>null
));

so we can place the radio button anywhere on the page.

regards

nintriva

Total 4 comments

#18003 report it
Sohail Ahmad at 2014/08/25 01:43pm
Helped out, time saving

Thank you boy, !!! saved my time

#14055 report it
Karl Zilles at 2013/07/15 08:18pm
Thanks!

Very helpful.

#11607 report it
Sebastian at 2013/01/22 08:41pm
Logic "bug" fixed

Just write it like this:

echo CHtml::radioButton('btn', false, array(
    'value'=>'1',
    'id'=>'btnname1',
    'uncheckValue'=>null
)); 
CHtml::radioButton('btn', false, array(
    'value'=>'2',
    'id'=>'btnname2',
    'uncheckValue'=>null
));
#9970 report it
marianzburlea at 2012/09/25 09:10pm
that will generate a logic bug

it will generate two inputs with the same id="idname" which will lead to more trouble since id tags in HTML are intended to be unique

Leave a comment

Please to leave your comment.

Write new article