jwaddick
(Jwaddick)
October 9, 2012, 8:09pm
1
I have quite a few boolean fields in my various tables. The customer would like the forms to use Yes/No radio buttons for each field instead of a checkbox.
What is the best way to go about doing this, such that the true boolean maps to yes radio button?
Thanks
Janice
mentel
(Yii)
October 9, 2012, 11:13pm
2
The easy, quick, dirty way to do it: use hidden checkboxes and set their values with javascript upon change on the radio buttons.
You can use this on the view:
<?php echo CHtml::radioButton('name1', $model->attributeName); ?>
<?php echo CHtml::radioButton('name2', !$model->attributeName); ?>
You will still need to setup the labels, validation and handle the data received in the controller.
abennouna
(Abennouna)
October 9, 2012, 11:31pm
3
Like this?
<?php echo $form->radioButtonList($model, 'yourBooleanAttribute', array(1 => 'Yes', 0 => 'No'), array('separator' => '')); ?>
softark
(Softark)
October 9, 2012, 11:33pm
4
I think that CActiveForm::radioButtonList can be a good choice.
http://www.yiiframework.com/doc/api/1.1/CActiveForm#radioButtonList-detail
$yesNo = array('0' => 'No', '1' => 'Yes');
echo $form->labelEx($model, 'name');
echo $form->radioButtonList($model, 'name', $yesNo);
echo $form->error($model, 'name');
mentel
(Yii)
October 9, 2012, 11:54pm
5
Agreed, guys. radioButtonList is a better choice.