Skip to content

Commit

Permalink
DivDecorator: Add support for FieldsetElement
Browse files Browse the repository at this point in the history
  • Loading branch information
nilmerg authored and Timm Ortloff committed Jan 12, 2023
1 parent dad178c commit 92d1181
Showing 1 changed file with 30 additions and 12 deletions.
42 changes: 30 additions & 12 deletions src/FormDecorator/DivDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -86,7 +87,7 @@ protected function assembleElement()
$this->formElement->getAttributes()->set('aria-required', 'true');
}

return $this->formElement;
return $this->formElement->ensureAssembled();
}

protected function assembleErrors()
Expand All @@ -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;
Expand All @@ -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()
]));
}
}
}

0 comments on commit 92d1181

Please sign in to comment.