Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Radio Button group in separate place in yii2

So, I want to have two radio button in separate place, I have been trying to search for the solution and everyone suggests to use radiolist which is not possible in my case.

If I put it like this (work_part_time button) : (below)

<div class="row">
    <div class="col-sm-2">
        <?= $form->field($model, 'work_part_time')->radio(['label' => 'yes', 'value' => 1])?>
    </div>-

    <div class="col-sm-3">
        <?= $form->field($model, 'hour_week')->textInput(['type' => 'number', 'placeholder' => 'Hour/Week'])->label(false)?>
    </div>

    <div class="col-sm-3">
        <?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->label(false)?> 
    </div>
</div>

<div class="form-group">
    <?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
</div>
<hr>
<div class="row">
    <div class="col-sm-2">
        <?= $form->field($model, 'work_part_time')->radio(['label' => 'yes', 'value' => 1])?>
    </div>-

    <div class="col-sm-3">
        <?= $form->field($model, 'hour_week')->textInput(['type' => 'number', 'placeholder' => 'Hour/Week'])->label(false)?>
    </div>

    <div class="col-sm-3">
        <?= $form->field($model, 'part_time_rate')->textInput(['type' => 'number', 'placeholder' => 'rate/hour(SGD)'])->label(false)?> 
    </div>
</div>

<div class="form-group">
    <?= $form->field($model, 'work_part_time')->radio( [0 => 'No'])->label('No')?>
</div>
<hr>

I only can get 0 for the value.

Anyone has found the solution for this?

like image 599
Robert Limanto Avatar asked Dec 11 '15 04:12

Robert Limanto


1 Answers

Yii will assign a checked or unchecked value to the radio button depending on the value of the stored attribute, so if the value is 0 it will check the button that has the value 0. Your problem seems to have been the hidden input that Yii automatically generates. As others have suggested, you need to set this to null if you want more than one radio button for the same field.

If the user checks another button, then all other radio buttons with the same name will become unchecked. The name of the attribute is generated automatically by Yii when it creates the button. Try these for your radio buttons:

<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 1', 'value' => 1, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 2', 'value' => 0, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option 3', 'value' => 2, 'uncheck' => null]) ?>
<?= $form->field($model, 'work_part_time')->radio(['label' => 'Option4', 'value' => 3, 'uncheck' => null]) ?>

Each button needs a different value, and this is the value that will be stored in your field when the record is saved.

There can only ever be one button checked, so if you have multiple buttons with the same value, and the same name, as you seem to have in your examples, then only the last one in the set will be checked. I don't know of a way round this. I suggest you use <formgroup> to split up your form into logical sections, each section relating to whether work_part_time is yes or no. You seem to have started doing this!

like image 83
Joe Miller Avatar answered Sep 20 '22 23:09

Joe Miller