Nikita Platonenko
Nikita Platonenko

Reputation: 702

Yii radio buttons issue

i generate radio buttons using Yii the code looks like

    <span class="gender">
       <?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'male')); ?>
    </span>

  <span class="gender">
       <?php echo CHtml::activeRadioButton($model,'gender',array('value' => 'female')); ?>
  </span>

It's generate next HTML code

    <span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">

<input value="male" class="male" name="web\models\register[gender]"id="web\models\register_gender" type="radio"></span>

<span class="gender">
<input id="ytweb\models\register_gender" type="hidden" value="0" name="web\models\register[gender]">

<input value="female" class="female" name="web\models\register[gender]" id="web\models\register_gender" type="radio">

</span>

when i get POST from this form, if i checked female it returns female, but if i cheked male it returns 0. I think it is because of identical inputs ids. But how can i avoid it?

Upvotes: 1

Views: 4229

Answers (1)

Jon
Jon

Reputation: 437376

You can configure activeRadioButton to not output the "guard input" with value 0. Do this by passing the parameter 'uncheckValue' => null as part of your options array:

echo CHtml::activeRadioButton($model,'gender',
    array('value' => 'male', 'uncheckValue' => null));
echo CHtml::activeRadioButton($model,'gender',
   array('value' => 'female', 'uncheckValue' => null));

That said, using activeRadioButtonList is the best choice here. You can configure its template parameter to specify your custom HTML:

$genders = array('male' => 'male', 'female' => 'female');
$options = array(
    'template'     => '<span class="gender">{input</span>',
    'uncheckValue' => null,
);
echo CHtml::activeRadioButtonList($model, 'genger', $genders, $options);

Upvotes: 5

Related Questions