Boolean Yes/no Radio Buttons

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?



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.

Like this?

<?php echo $form->radioButtonList($model, 'yourBooleanAttribute', array(1 => 'Yes', 0 => 'No'), array('separator' => '')); ?>

I think that CActiveForm::radioButtonList can be a good choice.

$yesNo = array('0' => 'No', '1' => 'Yes');

echo $form->labelEx($model, 'name');

echo $form->radioButtonList($model, 'name', $yesNo);

echo $form->error($model, 'name');

Agreed, guys. radioButtonList is a better choice.