-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
Open
Labels
22Yii 2.2Yii 2.2
Description
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,
);
yii2/framework/widgets/ActiveField.php
Line 614 in de3bd9e
$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.',
);
}
xicond
Metadata
Metadata
Assignees
Labels
22Yii 2.2Yii 2.2