diff --git a/CHANGELOG.md b/CHANGELOG.md
index 242aaa89..33b35e54 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -4,6 +4,7 @@
- New #278: Add dedicated classes for missing HTML tags (@Mister-42)
- New #279: Add support for conditional CSS classes in `Html::addCssClass()` (@Mister-42)
+- Enh #285: Add optional `$buttonFactory` parameter to `ButtonGroup::buttonsData()` (@Mister-42)
## 4.2.0 June 05, 2026
diff --git a/src/Widget/ButtonGroup.php b/src/Widget/ButtonGroup.php
index 76eb0b63..51da3c1f 100644
--- a/src/Widget/ButtonGroup.php
+++ b/src/Widget/ButtonGroup.php
@@ -9,6 +9,7 @@
use Yiisoft\Html\NoEncodeStringableInterface;
use Yiisoft\Html\Tag\Button;
+use function assert;
use function is_array;
use function is_string;
@@ -80,11 +81,20 @@ public function buttons(Button ...$buttons): self
* ]
* ```
* @param bool $encode Whether button content should be HTML-encoded.
+ * @param callable|null $buttonFactory Optional callback to create a Button from each data row.
+ * Signature: `function(array $item, bool $encode): Button`. When provided, the default
+ * label/type validation is skipped and the factory is fully responsible for building buttons.
*/
- public function buttonsData(array $data, bool $encode = true): self
+ public function buttonsData(array $data, bool $encode = true, ?callable $buttonFactory = null): self
{
$buttons = [];
foreach ($data as $row) {
+ if ($buttonFactory !== null) {
+ $button = $buttonFactory($row, $encode);
+ assert($button instanceof Button);
+ $buttons[] = $button;
+ continue;
+ }
if (!is_array($row) || !isset($row[0]) || !is_string($row[0])) {
throw new InvalidArgumentException(
'Invalid buttons data. A data row must be array with label as first element '
diff --git a/tests/Widget/ButtonGroupTest.php b/tests/Widget/ButtonGroupTest.php
index 1ad4e8f4..3f77fbe0 100644
--- a/tests/Widget/ButtonGroupTest.php
+++ b/tests/Widget/ButtonGroupTest.php
@@ -8,6 +8,7 @@
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\TestCase;
use Yiisoft\Html\Html;
+use Yiisoft\Html\Tag\Button;
use Yiisoft\Html\Widget\ButtonGroup;
final class ButtonGroupTest extends TestCase
@@ -371,6 +372,82 @@ public function testStringable(): void
$this->assertStringContainsStringIgnoringLineEndings($widget->render(), (string) $widget);
}
+ public function testButtonsDataWithFactory(): void
+ {
+ $factory = static function (array $item, bool $encode): Button {
+ $label = $item[0] ?? null;
+ unset($item[0]);
+ return Html::button((string) $label, $item)->encode($encode);
+ };
+
+ $widget = (new ButtonGroup())
+ ->buttonsData([
+ ['Reset Data', 'type' => 'reset'],
+ ['Send >', 'type' => 'submit', 'class' => 'primary'],
+ ], buttonFactory: $factory);
+
+ $this->assertStringContainsStringIgnoringLineEndings(
+ <<
+
+
+
+ HTML,
+ $widget->render(),
+ );
+ }
+
+ public function testButtonsDataWithFactorySkipsValidation(): void
+ {
+ $factory = static function (array $item, bool $encode): Button {
+ $label = $item[0] ?? null;
+ unset($item[0]);
+ return Html::button((string) $label, $item)->encode($encode);
+ };
+
+ $widget = (new ButtonGroup())
+ ->buttonsData([[42]], buttonFactory: $factory);
+
+ $this->assertStringContainsStringIgnoringLineEndings(
+ '',
+ $widget->render(),
+ );
+ }
+
+ public function testButtonsDataWithFactoryNullLabel(): void
+ {
+ $factory = static function (array $item, bool $encode): Button {
+ $label = $item[0] ?? null;
+ unset($item[0]);
+ return Html::button((string) $label, $item)->encode($encode);
+ };
+
+ $widget = (new ButtonGroup())
+ ->buttonsData([[null, 'type' => 'submit']], buttonFactory: $factory);
+
+ $this->assertStringContainsStringIgnoringLineEndings(
+ '',
+ $widget->render(),
+ );
+ }
+
+ public function testButtonsDataWithFactoryEncodeFalse(): void
+ {
+ $factory = static function (array $item, bool $encode): Button {
+ $label = $item[0] ?? null;
+ unset($item[0]);
+ return Html::button((string) $label, $item)->encode($encode);
+ };
+
+ $widget = (new ButtonGroup())
+ ->buttonsData([['Bold']], false, buttonFactory: $factory);
+
+ $this->assertStringContainsStringIgnoringLineEndings(
+ '',
+ $widget->render(),
+ );
+ }
+
public function testImmutability(): void
{
$widget = new ButtonGroup();