diff --git a/src/FormElement/FieldsetElement.php b/src/FormElement/FieldsetElement.php index eca6caae..6980cfa9 100644 --- a/src/FormElement/FieldsetElement.php +++ b/src/FormElement/FieldsetElement.php @@ -5,6 +5,8 @@ use ipl\Html\Attribute; use ipl\Html\Attributes; use ipl\Html\Contract\FormElement; +use ipl\Html\Contract\FormElementDecorator; +use ipl\Html\Contract\Wrappable; use LogicException; use ReflectionProperty; @@ -55,6 +57,19 @@ public function getValueAttribute() return null; } + public function prependWrapper(Wrappable $wrapper) + { + // TODO(lippserd): Revise decorator implementation to properly implement decorator propagation + if ( + ! $this->hasDefaultElementDecorator() + && $wrapper instanceof FormElementDecorator + ) { + $this->setDefaultElementDecorator($wrapper); + } + + return parent::prependWrapper($wrapper); + } + protected function onElementRegistered(FormElement $element) { $element->getAttributes()->registerAttributeCallback('name', function () use ($element) { diff --git a/tests/FormElement/FieldsetElementTest.php b/tests/FormElement/FieldsetElementTest.php index 5ba3dec1..1eeb0f35 100644 --- a/tests/FormElement/FieldsetElementTest.php +++ b/tests/FormElement/FieldsetElementTest.php @@ -5,6 +5,7 @@ use ipl\Html\Form; use ipl\Html\FormElement\FieldsetElement; use ipl\Tests\Html\TestCase; +use ipl\Tests\Html\TestDummy\SimpleFormElementDecorator; class FieldsetElementTest extends TestCase { @@ -120,4 +121,26 @@ public function testOriginalElementNames(): void $outer->getElement('inner')->getElement('test_select')->getName() ); } + + public function testDecoratorPropagation(): void + { + // Order is important because addElement() calls decorate(). Could be fixed. + $fieldset = (new FieldsetElement('test_fieldset')); + (new Form()) + ->setDefaultElementDecorator(new SimpleFormElementDecorator()) + ->addElement($fieldset); + $fieldset->addElement('select', 'test_select'); + + $expected = <<<'HTML' +
+
+
+ +
+
+
+HTML; + + $this->assertHtml($expected, $fieldset); + } }