Skip to content

ActiveField: Enhance label() method with tag option and fix labelOptions for checkbox/radio. #20537

@terabytesoftw

Description

@terabytesoftw

What steps will reproduce the problem?

When using ActiveField::checkbox() or ActiveField::radio() methods with $enclosedByLabel = false and providing a custom label string, the labelOptions array is completely ignored.

$field->checkbox(
    [
        'label' => 'Custom Label',
        // This is ignored
        'labelOptions' => [
            'class' => 'custom-class',
            'data-test' => 'value',
            'tag' => 'span',
        ],
    ],
    false,
);

$this->parts['{label}'] = $options['label'];

##What is the expected result?

The labelOptions should be applied to the label element, allowing developers to:

  • Add CSS classes and HTML attributes to the label.
  • Change the label tag (for example, from <label> to <span>).
  • Use tag => false to render only the label text without any wrapper.
public function testCheckboxEnclosedByLabelFalseWithCustomLabel(): void
{
    $this->activeField->checkbox(
        [
            'label' => 'Custom Label',
            'labelOptions' => [
                'class' => 'custom-label-class',
                'data-test' => 'custom-label-data',
            ],
        ],
        false,
    );

    $this->assertEqualsWithoutLE(
        <<<HTML
        <div class="form-group field-activefieldtestmodel-attributename">
        <label class="custom-label-class" data-test="custom-label-data" for="activefieldtestmodel-attributename">Custom Label</label>
        <input type="hidden" name="ActiveFieldTestModel[attributeName]" value="0"><input type="checkbox" id="activefieldtestmodel-attributename" name="ActiveFieldTestModel[attributeName]" value="1">
        <div class="hint-block">Hint for attributeName attribute</div>
        <div class="help-block"></div>
        </div>
        HTML,
        $this->activeField->render(),
        'Failed asserting that checkbox renders correctly.',
    );
}

public function testCheckboxEnclosedByLabelFalseWithCustomLabelTag(): void
{
    $this->activeField->checkbox(
        [
            'label' => 'Custom Label',
            'labelOptions' => [
                'class' => 'custom-label-class',
                'data-test' => 'custom-label-data',
                'tag' => 'span',
            ],
        ],
        false,
    );

    $this->assertEqualsWithoutLE(
        <<<HTML
        <div class="form-group field-activefieldtestmodel-attributename">
        <span class="custom-label-class" data-test="custom-label-data">Custom Label</span>
        <input type="hidden" name="ActiveFieldTestModel[attributeName]" value="0"><input type="checkbox" id="activefieldtestmodel-attributename" name="ActiveFieldTestModel[attributeName]" value="1">
        <div class="hint-block">Hint for attributeName attribute</div>
        <div class="help-block"></div>
        </div>
        HTML,
        $this->activeField->render(),
        'Failed asserting that checkbox renders correctly.',
    );
}

public function testCheckboxEnclosedByLabelFalseWithCustomLabelTagFalse(): void
{
    $this->activeField->checkbox(
        [
            'label' => 'Custom Label',
            'labelOptions' => [
                'tag' => false,
            ],
        ],
        false,
    );

    $this->assertEqualsWithoutLE(
        <<<HTML
        <div class="form-group field-activefieldtestmodel-attributename">
        Custom Label
        <input type="hidden" name="ActiveFieldTestModel[attributeName]" value="0"><input type="checkbox" id="activefieldtestmodel-attributename" name="ActiveFieldTestModel[attributeName]" value="1">
        <div class="hint-block">Hint for attributeName attribute</div>
        <div class="help-block"></div>
        </div>
        HTML,
        $this->activeField->render(),
        'Failed asserting that checkbox renders correctly.',
    );
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    22Yii 2.2

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions