Yii: Radio Button give 0 value

1.4k views Asked by At

I am using radio buttons to select correct option from number of possible options. I have four options to a question statement at the moment but possible answer option can increase. Problem i am facing is that following code only works for the last option else it gives 0 value at form submit. Also please suggest how can i use radioButtonList with following code as I want to show radio buttons in different DIVs.Please check following code and advice. Thanks in Advance

<div class="form-group" id="statement_div">
    <?php echo $form->labelEx($model,'statement', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'statement',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control')); ?>
        <?php echo $form->error($model,'statement'); ?>
    </div>

    <div class="clearfix"></div>
</div>


<div class="form-group" id="option1_div">
    <?php echo $form->labelEx($model,'option_1', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_1',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_1'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'1')) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option2_div">
    <?php echo $form->labelEx($model,'option_2', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_2',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_2'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'2')) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option3_div">
    <?php echo $form->labelEx($model,'option_3', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_3',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_3'); ?>
    </div>
     <?php echo $form->radioButton($model,'correct_option',array('value'=>'3')) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option4_div">
    <?php echo $form->labelEx($model,'option_4', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_4',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_4'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'4')) . ''; ?>
    <div class="clearfix"></div>
</div>
2

There are 2 answers

0
Samuel Liew On

If you are not using a radioButtonList, you will have to set each radioButton's uncheckValue to null.

http://www.yiiframework.com/wiki/315/using-multiple-radio-button/

<div class="form-group" id="statement_div">
    <?php echo $form->labelEx($model,'statement', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'statement',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control')); ?>
        <?php echo $form->error($model,'statement'); ?>
    </div>

    <div class="clearfix"></div>
</div>


<div class="form-group" id="option1_div">
    <?php echo $form->labelEx($model,'option_1', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_1',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_1'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'1','uncheckValue' => null)); ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option2_div">
    <?php echo $form->labelEx($model,'option_2', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_2',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_2'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'2','uncheckValue' => null))) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option3_div">
    <?php echo $form->labelEx($model,'option_3', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_3',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control', 'readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_3'); ?>
    </div>
     <?php echo $form->radioButton($model,'correct_option',array('value'=>'3','uncheckValue' => null))) . ''; ?>

    <div class="clearfix"></div>
</div>

<div class="form-group" id="option4_div">
    <?php echo $form->labelEx($model,'option_4', array('class' => 'col-md-3 control-label', 'id'=>'question_label')); ?>
    <div class="col-md-9">
        <?php echo $form->textArea($model,'option_4',array('rows'=>3, 'cols'=>50, 'class'=> 'form-control','readonly'=>$readonly)); ?>


        <?php echo $form->error($model,'option_4'); ?>
    </div>
    <?php echo $form->radioButton($model,'correct_option',array('value'=>'4','uncheckValue' => null))) . ''; ?>
    <div class="clearfix"></div>
</div>
0
Justinas On

It's because activeRadioButton is generating invincible input field with same name for each checkbox/radio (so previous values are overwritten by this field in form data). Apply uncheckValue => null to remove this field. Just keep in mind that without this field if use do not select any radio/checkbox than in form data there will be no key correct_option.

<?php
echo $form->radioButton(
    $model,
    'correct_option',
    array(
        'value' => '4',
        'uncheckValue' => null,
    )
);
?>