From d8a8e25c050ef1f6f2354964cc917ad3565d45cd Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 04:41:22 +0200 Subject: [PATCH 01/19] Add ButtonGroup::buttonsFieldData() method --- src/Field/ButtonGroup.php | 56 ++++++++++++++++ tests/Field/ButtonGroupTest.php | 109 ++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 709a827e..5ec2f0cd 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -8,6 +8,8 @@ use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Html\Tag\Button as ButtonTag; use Yiisoft\Html\Widget\ButtonGroup as ButtonGroupWidget; +use Yiisoft\Form\Field\ResetButton; +use Yiisoft\Form\Field\SubmitButton; /** * Represents a button group widget. @@ -56,6 +58,60 @@ public function buttonsData(array $data, bool $encode = true): self return $new; } + /** + * Creates buttons from array data using {@see ButtonField} instances instead of raw tag buttons. + * + * Each button is an array with label as first element and additional name-value pairs as attributes. + * The `type` attribute determines which ButtonField subclass is used: + * - `reset` → {@see ResetButton} + * - `submit` → {@see SubmitButton} + * - default → {@see Button} + * + * @param array $data Array of buttons. Each button is an array with label as first element and additional + * name-value pairs as attributes of button. + * + * Example: + * ```php + * [ + * ['Reset', 'type' => 'reset', 'class' => 'default'], + * ['Send', 'type' => 'submit', 'class' => 'primary'], + * ] + * ``` + * @param bool $encode Whether button content should be HTML-encoded. + */ + public function buttonsFieldData(array $data, bool $encode = true): self + { + $buttons = []; + foreach ($data as $item) { + $label = $item[0] ?? ''; + $attributes = array_slice($item, 1, null, true); + + $type = $attributes['type'] ?? 'button'; + unset($attributes['type']); + + $buttonField = match ($type) { + 'reset' => ResetButton::widget(), + 'submit' => SubmitButton::widget(), + default => Button::widget(), + }; + + $buttonField = $buttonField + ->content($label) + ->encodeContent(false) + ->addButtonAttributes($attributes); + + $buttonTag = $buttonField->getButton(); + + if (!$encode) { + $buttonTag = $buttonTag->encode(false); + } + + $buttons[] = $buttonTag; + } + + return $this->buttons(...$buttons); + } + public function buttonAttributes(array $attributes): self { $new = clone $this; diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 1bca1e14..04a37ee2 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -297,12 +297,121 @@ public function testButtonsWithFieldButtonCustomized(): void $this->assertSame($expected, $result); } + public function testButtonsFieldData(): void + { + $result = ButtonGroup::widget() + ->buttonsFieldData([ + ['Reset', 'type' => 'reset', 'class' => 'default'], + ['Send >', 'type' => 'submit', 'class' => 'primary'], + ]) + ->render(); + + $expected = << + + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsFieldDataDefaultType(): void + { + $result = ButtonGroup::widget() + ->buttonsFieldData([ + ['Click', 'class' => 'btn'], + ]) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsFieldDataNoEncode(): void + { + $result = ButtonGroup::widget() + ->buttonsFieldData([ + ['Bold', 'type' => 'submit'], + ], false) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsFieldDataEmpty(): void + { + $result = ButtonGroup::widget() + ->buttonsFieldData([]) + ->render(); + + $expected = << + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsFieldDataWithTheme(): void + { + ThemeContainer::initialize( + [ + 'default' => [ + 'fieldConfigs' => [ + ButtonGroup::class => [ + 'containerClass()' => ['btn-toolbar justify-content-end'], + ], + Button::class => [ + 'buttonClass()' => ['btn', 'btn-default'], + ], + ResetButton::class => [ + 'buttonClass()' => ['btn', 'btn-secondary'], + ], + SubmitButton::class => [ + 'buttonClass()' => ['btn', 'btn-primary'], + ], + ], + ], + ], + 'default', + ); + + $result = Field::ButtonGroup() + ->buttonsFieldData([ + ['Reset', 'type' => 'reset'], + ['Send', 'type' => 'submit'], + ]) + ->render(); + + $expected = << + + + + HTML; + + $this->assertSame($expected, $result); + } + public function testImmutability(): void { $field = ButtonGroup::widget(); $this->assertNotSame($field, $field->buttons()); $this->assertNotSame($field, $field->buttonsData([])); + $this->assertNotSame($field, $field->buttonsFieldData([])); $this->assertNotSame($field, $field->buttonAttributes([])); $this->assertNotSame($field, $field->addButtonAttributes([])); $this->assertNotSame($field, $field->disabled()); From 7458bde6a1c01c5e83446d22d07c5a8c7dcda533 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 04:42:57 +0200 Subject: [PATCH 02/19] Add changelog entry for #399 --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 662221fb..a9af3fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - New #388: Add `beforeRadio()` and `afterRadio()` methods to `RadioList` field (@vjik) - Enh #386: Bump minimal `yiisoft/html` version to `^4.2` (@vjik) - Enh #392: Move `.meta-storm.xml` to the project root (@Mister-42) +- Enh #399: Add `ButtonGroup::buttonsFieldData()` method (@va108) ## 1.5.2 March 20, 2026 From 49c5d827c082a2475aecf3f13f1f32a3fc4d57bf Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 04:44:31 +0200 Subject: [PATCH 03/19] Fix psalm errors and update changelog attribution --- CHANGELOG.md | 2 +- src/Field/ButtonGroup.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a9af3fd5..e5833999 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - New #388: Add `beforeRadio()` and `afterRadio()` methods to `RadioList` field (@vjik) - Enh #386: Bump minimal `yiisoft/html` version to `^4.2` (@vjik) - Enh #392: Move `.meta-storm.xml` to the project root (@Mister-42) -- Enh #399: Add `ButtonGroup::buttonsFieldData()` method (@va108) +- Enh #399: Add `ButtonGroup::buttonsFieldData()` method (@Mister-42) ## 1.5.2 March 20, 2026 diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 5ec2f0cd..f4a87edf 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -67,7 +67,7 @@ public function buttonsData(array $data, bool $encode = true): self * - `submit` → {@see SubmitButton} * - default → {@see Button} * - * @param array $data Array of buttons. Each button is an array with label as first element and additional + * @param list $data Array of buttons. Each button is an array with label as first element and additional * name-value pairs as attributes of button. * * Example: @@ -83,7 +83,7 @@ public function buttonsFieldData(array $data, bool $encode = true): self { $buttons = []; foreach ($data as $item) { - $label = $item[0] ?? ''; + $label = (string) ($item[0] ?? ''); $attributes = array_slice($item, 1, null, true); $type = $attributes['type'] ?? 'button'; From a4357b3c8d42acac57448a73e308f613f7e3b404 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 04:47:10 +0200 Subject: [PATCH 04/19] Add integer label test to kill Infection mutant --- tests/Field/ButtonGroupTest.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 04a37ee2..fa61f402 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -364,6 +364,23 @@ public function testButtonsFieldDataEmpty(): void $this->assertSame($expected, $result); } + public function testButtonsFieldDataIntegerLabel(): void + { + $result = ButtonGroup::widget() + ->buttonsFieldData([ + [0, 'type' => 'submit'], + ]) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + public function testButtonsFieldDataWithTheme(): void { ThemeContainer::initialize( From c272ca2a24270fd66a1abe16a8fa77eb21369410 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 04:51:59 +0200 Subject: [PATCH 05/19] Run php-cs-fixer fix --- src/Field/ButtonGroup.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index f4a87edf..4af81367 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -8,8 +8,8 @@ use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Html\Tag\Button as ButtonTag; use Yiisoft\Html\Widget\ButtonGroup as ButtonGroupWidget; -use Yiisoft\Form\Field\ResetButton; -use Yiisoft\Form\Field\SubmitButton; + +use function array_slice; /** * Represents a button group widget. From c4ff332995f86b967cd2f9d5058ccc3e79c9f756 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 05:07:07 +0200 Subject: [PATCH 06/19] Allow null label in buttonsFieldData to use themed content --- src/Field/ButtonGroup.php | 9 ++++++--- tests/Field/ButtonGroupTest.php | 11 +++++------ 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 4af81367..f9442677 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -68,7 +68,7 @@ public function buttonsData(array $data, bool $encode = true): self * - default → {@see Button} * * @param list $data Array of buttons. Each button is an array with label as first element and additional - * name-value pairs as attributes of button. + * name-value pairs as attributes of button. If label is null, the content from the theme is used. * * Example: * ```php @@ -83,7 +83,7 @@ public function buttonsFieldData(array $data, bool $encode = true): self { $buttons = []; foreach ($data as $item) { - $label = (string) ($item[0] ?? ''); + $label = $item[0] ?? null; $attributes = array_slice($item, 1, null, true); $type = $attributes['type'] ?? 'button'; @@ -95,8 +95,11 @@ public function buttonsFieldData(array $data, bool $encode = true): self default => Button::widget(), }; + if ($label !== null) { + $buttonField = $buttonField->content((string) $label); + } + $buttonField = $buttonField - ->content($label) ->encodeContent(false) ->addButtonAttributes($attributes); diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index fa61f402..628b1cf6 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -390,14 +390,13 @@ public function testButtonsFieldDataWithTheme(): void ButtonGroup::class => [ 'containerClass()' => ['btn-toolbar justify-content-end'], ], - Button::class => [ - 'buttonClass()' => ['btn', 'btn-default'], - ], ResetButton::class => [ 'buttonClass()' => ['btn', 'btn-secondary'], + 'content()' => ['Reset'], ], SubmitButton::class => [ 'buttonClass()' => ['btn', 'btn-primary'], + 'content()' => ['Submit'], ], ], ], @@ -407,15 +406,15 @@ public function testButtonsFieldDataWithTheme(): void $result = Field::ButtonGroup() ->buttonsFieldData([ - ['Reset', 'type' => 'reset'], - ['Send', 'type' => 'submit'], + [null, 'type' => 'reset'], + [null, 'type' => 'submit'], ]) ->render(); $expected = << - + HTML; From f7beb046b41ecbc61c7b455d2038a5003f64bc88 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 12:33:00 +0200 Subject: [PATCH 07/19] Simplify encoding in buttonsFieldData() --- src/Field/ButtonGroup.php | 8 +------- tests/Field/ButtonGroupTest.php | 4 ++-- 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index f9442677..17a6ef19 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -103,13 +103,7 @@ public function buttonsFieldData(array $data, bool $encode = true): self ->encodeContent(false) ->addButtonAttributes($attributes); - $buttonTag = $buttonField->getButton(); - - if (!$encode) { - $buttonTag = $buttonTag->encode(false); - } - - $buttons[] = $buttonTag; + $buttons[] = $buttonField->getButton()->encode($encode); } return $this->buttons(...$buttons); diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 628b1cf6..57b4a29e 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -407,14 +407,14 @@ public function testButtonsFieldDataWithTheme(): void $result = Field::ButtonGroup() ->buttonsFieldData([ [null, 'type' => 'reset'], - [null, 'type' => 'submit'], + ['Send', 'type' => 'submit'], ]) ->render(); $expected = << - + HTML; From ea706ec8dbddb1ba1cd12f3c91ebbdd3c370f1bf Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 16:11:34 +0200 Subject: [PATCH 08/19] Replace buttonsFieldData() with buttonsData(..., themed: true) --- CHANGELOG.md | 2 +- src/Field/ButtonGroup.php | 90 ++++++++++++++------------------- tests/Field/ButtonGroupTest.php | 36 ++++++------- 3 files changed, 57 insertions(+), 71 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5833999..bad7a95b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - New #388: Add `beforeRadio()` and `afterRadio()` methods to `RadioList` field (@vjik) - Enh #386: Bump minimal `yiisoft/html` version to `^4.2` (@vjik) - Enh #392: Move `.meta-storm.xml` to the project root (@Mister-42) -- Enh #399: Add `ButtonGroup::buttonsFieldData()` method (@Mister-42) +- Enh #399: Add `$asWidget` parameter to `ButtonGroup::buttonsData()` method (@Mister-42) ## 1.5.2 March 20, 2026 diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 17a6ef19..aa4dd504 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -6,6 +6,7 @@ use Yiisoft\Form\Field\Base\ButtonField; use Yiisoft\Form\Field\Base\PartsField; +use Yiisoft\Form\Theme\ThemeContainer; use Yiisoft\Html\Tag\Button as ButtonTag; use Yiisoft\Html\Widget\ButtonGroup as ButtonGroupWidget; @@ -50,63 +51,48 @@ public function buttons(ButtonTag|ButtonField ...$buttons): self * ] * ``` * @param bool $encode Whether button content should be HTML-encoded. - */ - public function buttonsData(array $data, bool $encode = true): self - { - $new = clone $this; - $new->widget = $this->widget->buttonsData($data, $encode); - return $new; - } - - /** - * Creates buttons from array data using {@see ButtonField} instances instead of raw tag buttons. - * - * Each button is an array with label as first element and additional name-value pairs as attributes. - * The `type` attribute determines which ButtonField subclass is used: - * - `reset` → {@see ResetButton} - * - `submit` → {@see SubmitButton} - * - default → {@see Button} - * - * @param list $data Array of buttons. Each button is an array with label as first element and additional - * name-value pairs as attributes of button. If label is null, the content from the theme is used. + * @param bool $themed When true, creates buttons via {@see ButtonField} instances + * ({@see Button}, {@see ResetButton}, {@see SubmitButton}) instead of raw tag buttons. + * The `type` attribute determines which subclass is used: + * `reset` → {@see ResetButton}, `submit` → {@see SubmitButton}, default → {@see Button}. + * If label is null, the content from the theme is used. + * Theme styling only applies when the host application has configured a theme via + * {@see ThemeContainer}. Without a configured theme, the output is identical to the + * default (non-themed) behavior. * - * Example: - * ```php - * [ - * ['Reset', 'type' => 'reset', 'class' => 'default'], - * ['Send', 'type' => 'submit', 'class' => 'primary'], - * ] - * ``` - * @param bool $encode Whether button content should be HTML-encoded. + * In a future major version, the default value of this parameter will change to `true`. */ - public function buttonsFieldData(array $data, bool $encode = true): self + public function buttonsData(array $data, bool $encode = true, bool $themed = false): self { - $buttons = []; - foreach ($data as $item) { - $label = $item[0] ?? null; - $attributes = array_slice($item, 1, null, true); - - $type = $attributes['type'] ?? 'button'; - unset($attributes['type']); - - $buttonField = match ($type) { - 'reset' => ResetButton::widget(), - 'submit' => SubmitButton::widget(), - default => Button::widget(), - }; - - if ($label !== null) { - $buttonField = $buttonField->content((string) $label); + $factory = $themed + ? static function (array $item, bool $encode): ButtonTag { + $label = $item[0] ?? null; + $attributes = array_slice($item, 1, null, true); + + $type = $attributes['type'] ?? 'button'; + unset($attributes['type']); + + $buttonField = match ($type) { + 'reset' => ResetButton::widget(), + 'submit' => SubmitButton::widget(), + default => Button::widget(), + }; + + if ($label !== null) { + $buttonField = $buttonField->content((string) $label); + } + + return $buttonField + ->encodeContent(false) + ->addButtonAttributes($attributes) + ->getButton() + ->encode($encode); } + : null; - $buttonField = $buttonField - ->encodeContent(false) - ->addButtonAttributes($attributes); - - $buttons[] = $buttonField->getButton()->encode($encode); - } - - return $this->buttons(...$buttons); + $new = clone $this; + $new->widget = $this->widget->buttonsData($data, $encode, $factory); + return $new; } public function buttonAttributes(array $attributes): self diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 57b4a29e..1edf9838 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -297,13 +297,13 @@ public function testButtonsWithFieldButtonCustomized(): void $this->assertSame($expected, $result); } - public function testButtonsFieldData(): void + public function testButtonsDataThemed(): void { $result = ButtonGroup::widget() - ->buttonsFieldData([ + ->buttonsData([ ['Reset', 'type' => 'reset', 'class' => 'default'], ['Send >', 'type' => 'submit', 'class' => 'primary'], - ]) + ], themed: true) ->render(); $expected = <<assertSame($expected, $result); } - public function testButtonsFieldDataDefaultType(): void + public function testButtonsDataThemedDefaultType(): void { $result = ButtonGroup::widget() - ->buttonsFieldData([ + ->buttonsData([ ['Click', 'class' => 'btn'], - ]) + ], themed: true) ->render(); $expected = <<assertSame($expected, $result); } - public function testButtonsFieldDataNoEncode(): void + public function testButtonsDataThemedNoEncode(): void { $result = ButtonGroup::widget() - ->buttonsFieldData([ + ->buttonsData([ ['Bold', 'type' => 'submit'], - ], false) + ], false, themed: true) ->render(); $expected = <<assertSame($expected, $result); } - public function testButtonsFieldDataEmpty(): void + public function testButtonsDataThemedEmpty(): void { $result = ButtonGroup::widget() - ->buttonsFieldData([]) + ->buttonsData([], themed: true) ->render(); $expected = <<assertSame($expected, $result); } - public function testButtonsFieldDataIntegerLabel(): void + public function testButtonsDataThemedIntegerLabel(): void { $result = ButtonGroup::widget() - ->buttonsFieldData([ + ->buttonsData([ [0, 'type' => 'submit'], - ]) + ], themed: true) ->render(); $expected = <<assertSame($expected, $result); } - public function testButtonsFieldDataWithTheme(): void + public function testButtonsDataThemedWithTheme(): void { ThemeContainer::initialize( [ @@ -405,10 +405,10 @@ public function testButtonsFieldDataWithTheme(): void ); $result = Field::ButtonGroup() - ->buttonsFieldData([ + ->buttonsData([ [null, 'type' => 'reset'], ['Send', 'type' => 'submit'], - ]) + ], themed: true) ->render(); $expected = <<assertNotSame($field, $field->buttons()); $this->assertNotSame($field, $field->buttonsData([])); - $this->assertNotSame($field, $field->buttonsFieldData([])); + $this->assertNotSame($field, $field->buttonsData([], themed: true)); $this->assertNotSame($field, $field->buttonAttributes([])); $this->assertNotSame($field, $field->addButtonAttributes([])); $this->assertNotSame($field, $field->disabled()); From 3da439f7566a9da0a8c4cb0d7270c7b8ed6c9ea8 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 17:05:23 +0200 Subject: [PATCH 09/19] Fix changelog: rename to --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bad7a95b..d855aab4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - New #388: Add `beforeRadio()` and `afterRadio()` methods to `RadioList` field (@vjik) - Enh #386: Bump minimal `yiisoft/html` version to `^4.2` (@vjik) - Enh #392: Move `.meta-storm.xml` to the project root (@Mister-42) -- Enh #399: Add `$asWidget` parameter to `ButtonGroup::buttonsData()` method (@Mister-42) +- Enh #399: Add `$themed` parameter to `ButtonGroup::buttonsData()` method (@Mister-42) ## 1.5.2 March 20, 2026 From 62d1bc9fa3bff777b4f3f083fb886f746517ea7a Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Sun, 26 Jul 2026 17:18:30 +0200 Subject: [PATCH 10/19] Rename $buttonField to $buttonWidget in themed closure --- src/Field/ButtonGroup.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index aa4dd504..15ca2f81 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -72,17 +72,17 @@ public function buttonsData(array $data, bool $encode = true, bool $themed = fal $type = $attributes['type'] ?? 'button'; unset($attributes['type']); - $buttonField = match ($type) { + $buttonWidget = match ($type) { 'reset' => ResetButton::widget(), 'submit' => SubmitButton::widget(), default => Button::widget(), }; if ($label !== null) { - $buttonField = $buttonField->content((string) $label); + $buttonWidget = $buttonWidget->content((string) $label); } - return $buttonField + return $buttonWidget ->encodeContent(false) ->addButtonAttributes($attributes) ->getButton() From 30d93bb98cca5d7b20cf1f1ceb3ff44d70aa221f Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Mon, 27 Jul 2026 09:20:30 +0200 Subject: [PATCH 11/19] Rewrite buttonsData() themed path to build buttons locally Instead of passing a factory callback to the vendor ButtonGroup, build ButtonField widgets inline and pass Button tags to buttons(). No vendor changes needed. --- src/Field/ButtonGroup.php | 57 +++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 26 deletions(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 15ca2f81..8699df83 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -64,34 +64,39 @@ public function buttons(ButtonTag|ButtonField ...$buttons): self */ public function buttonsData(array $data, bool $encode = true, bool $themed = false): self { - $factory = $themed - ? static function (array $item, bool $encode): ButtonTag { - $label = $item[0] ?? null; - $attributes = array_slice($item, 1, null, true); - - $type = $attributes['type'] ?? 'button'; - unset($attributes['type']); - - $buttonWidget = match ($type) { - 'reset' => ResetButton::widget(), - 'submit' => SubmitButton::widget(), - default => Button::widget(), - }; - - if ($label !== null) { - $buttonWidget = $buttonWidget->content((string) $label); - } - - return $buttonWidget - ->encodeContent(false) - ->addButtonAttributes($attributes) - ->getButton() - ->encode($encode); + $new = clone $this; + + if (!$themed) { + $new->widget = $this->widget->buttonsData($data, $encode); + return $new; + } + + $buttonTags = []; + foreach ($data as $item) { + $label = $item[0] ?? null; + $attributes = array_slice($item, 1, null, true); + + $type = $attributes['type'] ?? 'button'; + unset($attributes['type']); + + $buttonWidget = match ($type) { + 'reset' => ResetButton::widget(), + 'submit' => SubmitButton::widget(), + default => Button::widget(), + }; + + if ($label !== null) { + $buttonWidget = $buttonWidget->content((string) $label); } - : null; - $new = clone $this; - $new->widget = $this->widget->buttonsData($data, $encode, $factory); + $buttonTags[] = $buttonWidget + ->encodeContent(false) + ->addButtonAttributes($attributes) + ->getButton() + ->encode($encode); + } + + $new->widget = $this->widget->buttons(...$buttonTags); return $new; } From 7197db169834860bb5268c5c1f595e3881da2270 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Mon, 27 Jul 2026 09:24:21 +0200 Subject: [PATCH 12/19] Fix psalm errors in themed buttonsData() path --- src/Field/ButtonGroup.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 8699df83..41f72389 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -73,6 +73,9 @@ public function buttonsData(array $data, bool $encode = true, bool $themed = fal $buttonTags = []; foreach ($data as $item) { + if (!is_array($item)) { + continue; + } $label = $item[0] ?? null; $attributes = array_slice($item, 1, null, true); From 8868cec059b2fc5a359181cfb902103107868367 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Mon, 27 Jul 2026 10:05:02 +0200 Subject: [PATCH 13/19] Fix infection mutants: invert is_array check, add two new tests - Invert is_array guard to eliminate uncovered continue mutant (MSI 100%) - Add test verifying default non-themed output even when theme is configured - Add test verifying non-array items are skipped in themed mode --- src/Field/ButtonGroup.php | 45 +++++++++++++------------ tests/Field/ButtonGroupTest.php | 58 +++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+), 23 deletions(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 41f72389..23c0b266 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -73,30 +73,29 @@ public function buttonsData(array $data, bool $encode = true, bool $themed = fal $buttonTags = []; foreach ($data as $item) { - if (!is_array($item)) { - continue; + if (is_array($item)) { + $label = $item[0] ?? null; + $attributes = array_slice($item, 1, null, true); + + $type = $attributes['type'] ?? 'button'; + unset($attributes['type']); + + $buttonWidget = match ($type) { + 'reset' => ResetButton::widget(), + 'submit' => SubmitButton::widget(), + default => Button::widget(), + }; + + if ($label !== null) { + $buttonWidget = $buttonWidget->content((string) $label); + } + + $buttonTags[] = $buttonWidget + ->encodeContent(false) + ->addButtonAttributes($attributes) + ->getButton() + ->encode($encode); } - $label = $item[0] ?? null; - $attributes = array_slice($item, 1, null, true); - - $type = $attributes['type'] ?? 'button'; - unset($attributes['type']); - - $buttonWidget = match ($type) { - 'reset' => ResetButton::widget(), - 'submit' => SubmitButton::widget(), - default => Button::widget(), - }; - - if ($label !== null) { - $buttonWidget = $buttonWidget->content((string) $label); - } - - $buttonTags[] = $buttonWidget - ->encodeContent(false) - ->addButtonAttributes($attributes) - ->getButton() - ->encode($encode); } $new->widget = $this->widget->buttons(...$buttonTags); diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 1edf9838..2772e3b3 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -421,6 +421,64 @@ public function testButtonsDataThemedWithTheme(): void $this->assertSame($expected, $result); } + public function testButtonsDataDefaultNotThemedWithTheme(): void + { + ThemeContainer::initialize( + [ + 'default' => [ + 'fieldConfigs' => [ + ButtonGroup::class => [ + 'containerClass()' => ['btn-toolbar justify-content-end'], + ], + ResetButton::class => [ + 'buttonClass()' => ['btn', 'btn-secondary'], + 'content()' => ['Reset'], + ], + SubmitButton::class => [ + 'buttonClass()' => ['btn', 'btn-primary'], + 'content()' => ['Submit'], + ], + ], + ], + ], + 'default', + ); + + $result = ButtonGroup::widget() + ->buttonsData([ + ['Reset', 'type' => 'reset'], + ['Send', 'type' => 'submit'], + ]) + ->render(); + + $expected = << + + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedSkipsNonArrayItems(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + 'invalid', + ['Send', 'type' => 'submit'], + ], themed: true) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + public function testImmutability(): void { $field = ButtonGroup::widget(); From d5b1171eac38a1c69031cf7f478da5effa3018fa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=98=D0=B2=D0=B0=D0=BD?= <68641750+Mister-42@users.noreply.github.com> Date: Mon, 27 Jul 2026 11:48:15 +0200 Subject: [PATCH 14/19] Update CHANGELOG.md Co-authored-by: Sergei Predvoditelev --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d855aab4..07496814 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ - New #388: Add `beforeRadio()` and `afterRadio()` methods to `RadioList` field (@vjik) - Enh #386: Bump minimal `yiisoft/html` version to `^4.2` (@vjik) - Enh #392: Move `.meta-storm.xml` to the project root (@Mister-42) -- Enh #399: Add `$themed` parameter to `ButtonGroup::buttonsData()` method (@Mister-42) +- New #399: Add `$themed` parameter to `ButtonGroup::buttonsData()` method (@Mister-42) ## 1.5.2 March 20, 2026 From 7771e6b6ad3af9601c205d3d532983b9f42e8a2e Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Mon, 27 Jul 2026 12:07:22 +0200 Subject: [PATCH 15/19] Make $type case-insensitive, add tests for edge cases - strtolower() for case-insensitive type matching (review r3656025758) - Add test for case-insensitive type (RESET, Submit) - Add test for integer type value to kill CastString mutant --- src/Field/ButtonGroup.php | 2 +- tests/Field/ButtonGroupTest.php | 36 +++++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 23c0b266..b02a98a3 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -77,7 +77,7 @@ public function buttonsData(array $data, bool $encode = true, bool $themed = fal $label = $item[0] ?? null; $attributes = array_slice($item, 1, null, true); - $type = $attributes['type'] ?? 'button'; + $type = strtolower((string) ($attributes['type'] ?? 'button')); unset($attributes['type']); $buttonWidget = match ($type) { diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 2772e3b3..fe4e3b23 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -333,6 +333,42 @@ public function testButtonsDataThemedDefaultType(): void $this->assertSame($expected, $result); } + public function testButtonsDataThemedCaseInsensitiveType(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + ['Reset', 'type' => 'RESET'], + ['Send', 'type' => 'Submit'], + ], themed: true) + ->render(); + + $expected = << + + + + HTML; + + $this->assertSame($expected, $result); + } + + public function testButtonsDataThemedIntegerType(): void + { + $result = ButtonGroup::widget() + ->buttonsData([ + ['Click', 'type' => 123], + ], themed: true) + ->render(); + + $expected = << + + + HTML; + + $this->assertSame($expected, $result); + } + public function testButtonsDataThemedNoEncode(): void { $result = ButtonGroup::widget() From 85158925e90ecd3e7e996d40aba3cbc1d79115dc Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Mon, 27 Jul 2026 14:03:30 +0200 Subject: [PATCH 16/19] php-cs-fixer result fix --- src/Field/ButtonGroup.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index b02a98a3..2c92250f 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -11,6 +11,7 @@ use Yiisoft\Html\Widget\ButtonGroup as ButtonGroupWidget; use function array_slice; +use function is_array; /** * Represents a button group widget. From 936161a6b2a646eb5407bb6ce4e423f4793e372a Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Mon, 27 Jul 2026 14:52:35 +0200 Subject: [PATCH 17/19] 4786969039 --- src/Field/ButtonGroup.php | 1 + tests/Field/ButtonGroupTest.php | 7 +++++++ 2 files changed, 8 insertions(+) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 2c92250f..3819ad7c 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -12,6 +12,7 @@ use function array_slice; use function is_array; +use function strtolower; /** * Represents a button group widget. diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index fe4e3b23..509a2663 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -426,6 +426,9 @@ public function testButtonsDataThemedWithTheme(): void ButtonGroup::class => [ 'containerClass()' => ['btn-toolbar justify-content-end'], ], + Button::class => [ + 'content()' => ['Click'], + ], ResetButton::class => [ 'buttonClass()' => ['btn', 'btn-secondary'], 'content()' => ['Reset'], @@ -442,6 +445,8 @@ public function testButtonsDataThemedWithTheme(): void $result = Field::ButtonGroup() ->buttonsData([ + [], + ['type' => 'button'], [null, 'type' => 'reset'], ['Send', 'type' => 'submit'], ], themed: true) @@ -449,6 +454,8 @@ public function testButtonsDataThemedWithTheme(): void $expected = << + + From b6711900cb822812d6871a3bb2272abd37f64daa Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Mon, 27 Jul 2026 14:55:59 +0200 Subject: [PATCH 18/19] Spacing --- tests/Field/ButtonGroupTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 509a2663..1f23da71 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -445,8 +445,8 @@ public function testButtonsDataThemedWithTheme(): void $result = Field::ButtonGroup() ->buttonsData([ - [], - ['type' => 'button'], + [], + ['type' => 'button'], [null, 'type' => 'reset'], ['Send', 'type' => 'submit'], ], themed: true) From 9b29ed04c89a2be16df5aa9eeeb048647bcf0155 Mon Sep 17 00:00:00 2001 From: "Mr. 42" Date: Tue, 28 Jul 2026 12:36:04 +0200 Subject: [PATCH 19/19] Throw InvalidArgumentException for non-array items in themed buttonsData() Keeps behavior consistent between themed and non-themed buttonsData(), per review feedback on #399. --- src/Field/ButtonGroup.php | 50 ++++++++++++++++++--------------- tests/Field/ButtonGroupTest.php | 18 ++++++------ 2 files changed, 36 insertions(+), 32 deletions(-) diff --git a/src/Field/ButtonGroup.php b/src/Field/ButtonGroup.php index 3819ad7c..13b379f3 100644 --- a/src/Field/ButtonGroup.php +++ b/src/Field/ButtonGroup.php @@ -4,6 +4,7 @@ namespace Yiisoft\Form\Field; +use InvalidArgumentException; use Yiisoft\Form\Field\Base\ButtonField; use Yiisoft\Form\Field\Base\PartsField; use Yiisoft\Form\Theme\ThemeContainer; @@ -75,29 +76,34 @@ public function buttonsData(array $data, bool $encode = true, bool $themed = fal $buttonTags = []; foreach ($data as $item) { - if (is_array($item)) { - $label = $item[0] ?? null; - $attributes = array_slice($item, 1, null, true); - - $type = strtolower((string) ($attributes['type'] ?? 'button')); - unset($attributes['type']); - - $buttonWidget = match ($type) { - 'reset' => ResetButton::widget(), - 'submit' => SubmitButton::widget(), - default => Button::widget(), - }; - - if ($label !== null) { - $buttonWidget = $buttonWidget->content((string) $label); - } - - $buttonTags[] = $buttonWidget - ->encodeContent(false) - ->addButtonAttributes($attributes) - ->getButton() - ->encode($encode); + if (!is_array($item)) { + throw new InvalidArgumentException( + 'Invalid buttons data. A data row must be array with label as first element ' + . 'and additional name-value pairs as attributes of button.', + ); } + + $label = $item[0] ?? null; + $attributes = array_slice($item, 1, null, true); + + $type = strtolower((string) ($attributes['type'] ?? 'button')); + unset($attributes['type']); + + $buttonWidget = match ($type) { + 'reset' => ResetButton::widget(), + 'submit' => SubmitButton::widget(), + default => Button::widget(), + }; + + if ($label !== null) { + $buttonWidget = $buttonWidget->content((string) $label); + } + + $buttonTags[] = $buttonWidget + ->encodeContent(false) + ->addButtonAttributes($attributes) + ->getButton() + ->encode($encode); } $new->widget = $this->widget->buttons(...$buttonTags); diff --git a/tests/Field/ButtonGroupTest.php b/tests/Field/ButtonGroupTest.php index 1f23da71..d452b5b2 100644 --- a/tests/Field/ButtonGroupTest.php +++ b/tests/Field/ButtonGroupTest.php @@ -4,6 +4,7 @@ namespace Yiisoft\Form\Tests\Field; +use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Yiisoft\Form\Field\Button; use Yiisoft\Form\Field\ButtonGroup; @@ -504,22 +505,19 @@ public function testButtonsDataDefaultNotThemedWithTheme(): void $this->assertSame($expected, $result); } - public function testButtonsDataThemedSkipsNonArrayItems(): void + public function testButtonsDataThemedThrowsExceptionForNonArrayItem(): void { - $result = ButtonGroup::widget() + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Invalid buttons data. A data row must be array with label as first element ' + . 'and additional name-value pairs as attributes of button.', + ); + ButtonGroup::widget() ->buttonsData([ 'invalid', ['Send', 'type' => 'submit'], ], themed: true) ->render(); - - $expected = << - - - HTML; - - $this->assertSame($expected, $result); } public function testImmutability(): void