YiiBooster icon indicating copy to clipboard operation
YiiBooster copied to clipboard

Removing a label block from an input field in TbActiveForm

Open jbryu opened this issue 10 years ago • 1 comments

I want to remove a label block of an input field in a horizontal TbActiveForm. For example, when I add the following checkbox input field to a horizontal form, I want to have the following [Expected Output] in HTML, but it returns [Actual Output].

[PHP Code]

echo $form->checkboxGroup($model, 'rememberMe', array('wrapperHtmlOptions' => array( 'labelOptions' => array('label' => false)));

[Expected Output]

<div class="form-group">
    <div class="col-md-9" style="width: 75%;">
        <div class="checkbox">
            <input name="UserLogin[rememberMe]" id="ytUserLogin_rememberMe" type="hidden" value="0">
            <label label="">
                <input name="UserLogin[rememberMe]" id="UserLogin_rememberMe" type="checkbox" value="1"> Stay signed in
            </label>
        </div>
    </div>
</div>

[Actual Output]

<div class="form-group">
    <span class="col-md-3"></span>
    <div class="col-md-9" style="width: 75%;">
        <div class="checkbox">
            <input name="UserLogin[rememberMe]" id="ytUserLogin_rememberMe" type="hidden" value="0">
            <label label="">
                <input name="UserLogin[rememberMe]" id="UserLogin_rememberMe" type="checkbox" value="1"> Stay signed in
            </label>
        </div>
    </div>
</div>

So, I added the following code at line# 1288 in TbActiveForm. https://github.com/clevertech/YiiBooster/blob/1fec2bfcd53e2bd2796215d655cbbdd0ed7cbedb/src/widgets/TbActiveForm.php#L1228

    protected function horizontalGroup(&$fieldData, &$model, &$attribute, &$options)
    {
        ...
        self::addCssClass($options['labelOptions'], 'col-md-3 control-label');
        if (isset($options['label'])) {
            if (!empty($options['label'])) {
                echo CHtml::label($options['label'], CHtml::activeId($model, $attribute), $options['labelOptions']);
            } elseif (isset($options['labelOptions']) && isset($options['labelOptions']['label']) && !$options['labelOptions']['label']) {
                echo '';
            } else {
                echo '<span class="col-md-3"></span>';
            }
        } else {
            echo $this->labelEx($model, $attribute, $options['labelOptions']);
        }
        ...
    }

Is this a proper way to remove the label block? If not, please suggest any solutions.

Thanks,

jbryu avatar Nov 12 '14 19:11 jbryu

wondering about this also...it seems odd that the span before the checkbox exists at all, since the transition to vertical (for mobile) for a checkbox does not put the label above the checkbox, it stays next to it....

PrplHaz4 avatar Dec 05 '14 03:12 PrplHaz4