Skip to content

Commit a799a06

Browse files
Fix ActiveField::checkbox()/radio() dropping label for extensions with enclosedByLabel=false. (#21057)
1 parent e3314a7 commit a799a06

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

framework/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ Yii Framework 2 Change Log
154154
- Bug #16043: Fix `yii\db\oci\Schema::findColumns()` to report `null` size for `DATE`, `TIMESTAMP` and `INTERVAL` columns instead of their internal storage byte length (terabytesoftw)
155155
- Bug #12763: Apply an explicitly configured PostgreSQL `defaultSchema` as the session `search_path` when opening the connection (terabytesoftw)
156156
- Bug #19852: Use the connection passed to `ActiveQuery` result methods for schema reflection and typecasting while populating records (terabytesoftw)
157+
- Bug #21110: Fix `ActiveField::checkbox()`/`radio()` dropping label for extensions with `enclosedByLabel=false` (terabytesoftw)
157158

158159
2.0.56 under development
159160
------------------------

framework/widgets/ActiveField.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -575,12 +575,12 @@ public function radio($options = [], $enclosedByLabel = true)
575575
$this->addAriaAttributes($options);
576576
$this->adjustLabelFor($options);
577577

578-
if (!$enclosedByLabel) {
578+
if ($enclosedByLabel || (array_key_exists('label', $options) && $options['label'] === false)) {
579+
$this->parts['{label}'] ??= '';
580+
} else {
579581
$options = $this->generateLabel($options);
580582
}
581583

582-
$this->parts['{label}'] ??= '';
583-
584584
$this->parts['{input}'] = Html::activeRadio($this->model, $this->attribute, $options);
585585

586586
return $this;
@@ -622,12 +622,12 @@ public function checkbox($options = [], $enclosedByLabel = true)
622622
$this->addAriaAttributes($options);
623623
$this->adjustLabelFor($options);
624624

625-
if (!$enclosedByLabel) {
625+
if ($enclosedByLabel || (array_key_exists('label', $options) && $options['label'] === false)) {
626+
$this->parts['{label}'] ??= '';
627+
} else {
626628
$options = $this->generateLabel($options);
627629
}
628630

629-
$this->parts['{label}'] ??= '';
630-
631631
$this->parts['{input}'] = Html::activeCheckbox($this->model, $this->attribute, $options);
632632

633633
return $this;

tests/framework/widgets/ActiveFieldTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,28 @@ public function testCheckboxEnclosedByLabelFalse(array $options, string $expecte
10281028
);
10291029
}
10301030

1031+
public function testRadioEnclosedByLabelFalseWithoutLabelOption(): void
1032+
{
1033+
$this->activeField->radio([], false);
1034+
1035+
self::assertArrayNotHasKey(
1036+
'{label}',
1037+
$this->activeField->parts,
1038+
'Label part must remain unset so render() generates it from the model.',
1039+
);
1040+
}
1041+
1042+
public function testCheckboxEnclosedByLabelFalseWithoutLabelOption(): void
1043+
{
1044+
$this->activeField->checkbox([], false);
1045+
1046+
self::assertArrayNotHasKey(
1047+
'{label}',
1048+
$this->activeField->parts,
1049+
'Label part must remain unset so render() generates it from the model.',
1050+
);
1051+
}
1052+
10311053
public function testRadioEnclosedByLabelFalsePreservesExistingLabel(): void
10321054
{
10331055
$this->activeField->label('Existing Label');

0 commit comments

Comments
 (0)