From 92d118146bd9e0849ebfd43b4c9a58cdd176e9ab Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Wed, 14 Dec 2022 13:13:11 +0100 Subject: [PATCH] DivDecorator: Add support for `FieldsetElement` --- src/FormDecorator/DivDecorator.php | 42 +++++++++++++++++++++--------- 1 file changed, 30 insertions(+), 12 deletions(-) diff --git a/src/FormDecorator/DivDecorator.php b/src/FormDecorator/DivDecorator.php index e12739c0..43883832 100644 --- a/src/FormDecorator/DivDecorator.php +++ b/src/FormDecorator/DivDecorator.php @@ -7,6 +7,7 @@ use ipl\Html\Contract\FormElement; use ipl\Html\Contract\FormElementDecorator; use ipl\Html\Contract\FormSubmitElement; +use ipl\Html\FormElement\FieldsetElement; use ipl\Html\FormElement\HiddenElement; use ipl\Html\Html; use ipl\Html\HtmlElement; @@ -86,7 +87,7 @@ protected function assembleElement() $this->formElement->getAttributes()->set('aria-required', 'true'); } - return $this->formElement; + return $this->formElement->ensureAssembled(); } protected function assembleErrors() @@ -111,12 +112,16 @@ protected function assembleLabel() $label = $this->formElement->getLabel(); if ($label !== null) { - $attributes = null; - if ($this->formElement->getAttributes()->has('id')) { - $attributes = new Attributes(['for' => $this->formElement->getAttributes()->get('id')->getValue()]); + if ($this->formElement instanceof FieldsetElement) { + return new HtmlElement('legend', null, Text::create($label)); + } else { + $attributes = null; + if ($this->formElement->getAttributes()->has('id')) { + $attributes = new Attributes(['for' => $this->formElement->getAttributes()->get('id')->getValue()]); + } + + return Html::tag('label', $attributes, $label); } - - return Html::tag('label', $attributes, $label); } return null; @@ -128,11 +133,24 @@ protected function assemble() $this->getAttributes()->add('class', static::ERROR_HINT_CLASS); } - $this->addHtml(...Html::wantHtmlList([ - $this->assembleLabel(), - $this->assembleElement(), - $this->assembleDescription(), - $this->assembleErrors() - ])); + if ($this->formElement instanceof FieldsetElement) { + $element = $this->assembleElement(); + $element->prependHtml(...Html::wantHtmlList([ + $this->assembleLabel(), + $this->assembleDescription() + ])); + + $this->addHtml(...Html::wantHtmlList([ + $element, + $this->assembleErrors() + ])); + } else { + $this->addHtml(...Html::wantHtmlList([ + $this->assembleLabel(), + $this->assembleElement(), + $this->assembleDescription(), + $this->assembleErrors() + ])); + } } }