Yii Framework Forum: Boolean Yes/no Radio Buttons - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Boolean Yes/no Radio Buttons How do I create them Rate Topic: -----

#1 User is offline   Janice 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 1
  • Joined: 20-September 12

Posted 09 October 2012 - 03:09 PM

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
0

#2 User is offline   Rodrigo Coelho 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 665
  • Joined: 05-August 10
  • Location:Rio de Janeiro, Brazil

Posted 09 October 2012 - 06:13 PM

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.
0

#3 User is offline   bennouna 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 1,166
  • Joined: 05-January 12
  • Location:Morocco

Posted 09 October 2012 - 06:31 PM

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

0

#4 User is offline   softark 

  • Keep It Simple
  • Yii
  • Group: Moderators
  • Posts: 2,084
  • Joined: 16-February 11
  • Location:Japan

Posted 09 October 2012 - 06:33 PM

I think that CActiveForm::radioButtonList can be a good choice.
http://www.yiiframew...ttonList-detail

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

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

0

#5 User is offline   Rodrigo Coelho 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 665
  • Joined: 05-August 10
  • Location:Rio de Janeiro, Brazil

Posted 09 October 2012 - 06:54 PM

Agreed, guys. radioButtonList is a better choice.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users