Yii2: Radio and Checkbox templates

Created on 24 Feb 2015  路  5Comments  路  Source: yiisoft/yii2

In trying to implement some different checkboxes which requires I slightly adjust the rendering of radio buttons and checkboxes. It seems that not all radio and checkbox templates are following a provided template. I dug through the code but can't seem to figure out where the label is being incorrectly added.

Here's an example radio button without any change to the usual template

<div class="radio">
    <label>
        <input type="hidden" name="TestingRelated[field_a]" value="0">
        <label>
            <input type="radio" id="testingrelated-field_a" name="TestingRelated[field_a]" value="1">
            Field A
        </label>
        Field A
    </label>
    <p class="help-block help-block-error"></p>
</div>

Here's the same field with a template I believe should produce the results I need ("<div class=\"radio\">\n{input}\n{label}\n{error}\n{hint}\n</div>")

<div class="radio">
    <input type="hidden" name="TestingRelated[field_a]" value="0">
    <label>
        <input type="radio" id="testingrelated-field_a" name="TestingRelated[field_a]" value="1">
        Field A
    </label>
    <label for="testingrelated-field_a">
        Field A
    </label>
    <p class="help-block help-block-error"></p>
</div>

Here's just rendering the input field to see what it does all by itself
With Template ("<div class=\"radio\">\n{input}\n</div>")

<div class="radio">
    <input type="hidden" name="TestingRelated[field_a]" value="0">
    <label>
        <input type="radio" id="testingrelated-field_a" name="TestingRelated[field_a]" value="1">
        Field A
    </label>
</div>

Why, when I only want the input field (or a specific template) does a label still get returned. The template in the second example above works correctly for checkboxes

<div class="checkbox">
    <input type="hidden" name="TestingRelated[updated_by]" value="0">
    <input type="checkbox" id="testingrelated-updated_by" name="TestingRelated[updated_by]" value="1">
    <label for="testingrelated-updated_by">Updated By</label>
    <p class="help-block help-block-error"></p>
</div>

As you can see, the same template produce vastly different results in radio buttons vs checkboxes. I will wait to see how this gets address before I being up similar issues with radioList and checkboxList. It seems when trying to remove these inputs from their label wrapper via a template, the template is not truly being honored.

Most helpful comment

You are welcome. Will help future searchers.

All 5 comments

Since checkboxes worked correctly I just compared the two and found this was the only difference. Changing the file in my local install worked so I'm updating it here.

Hi!

This code:

$form = ActiveForm::begin([]);
echo $form->field($model, 'name', ['template' => "<div class=\"radio\">\n{input}\n{label}\n{error}\n{hint}\n</div>"])->input('radio');

... renders:

<div class="radio">
    <input type="radio" id="meumodel-name" class="form-control" name="meuModel[name]">
    <label class="control-label" for="meumodel-name">Name</label>
    <div class="help-block"></div>
</div>

Hope it helps you while the issue is verified.

Thanks sidtj! Since I'm just playing around with Yii for the moment I have my local ActiveField file changed but if my pull request gets denied I'll keep that in mind--setting the template at the field level rather than the form level.

You are welcome. Will help future searchers.

Thanks!

Was this page helpful?
0 / 5 - 0 ratings