From d212a43cbf95197a5e65b75ef50753680eb144ec Mon Sep 17 00:00:00 2001 From: Johannes Meyer Date: Tue, 30 Apr 2024 16:33:46 +0200 Subject: [PATCH] FieldsetElement: Correctly handle elements with multiple values fixes #136 --- src/FormElement/FieldsetElement.php | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/FormElement/FieldsetElement.php b/src/FormElement/FieldsetElement.php index 0d70ea4c..9ce0722c 100644 --- a/src/FormElement/FieldsetElement.php +++ b/src/FormElement/FieldsetElement.php @@ -3,6 +3,7 @@ namespace ipl\Html\FormElement; use InvalidArgumentException; +use ipl\Html\Common\MultipleAttribute; use ipl\Html\Contract\FormElement; use ipl\Html\Contract\FormElementDecorator; use ipl\Html\Contract\Wrappable; @@ -107,15 +108,23 @@ public function setWrapper(Wrappable $wrapper) protected function onElementRegistered(FormElement $element) { $element->getAttributes()->registerAttributeCallback('name', function () use ($element) { + $multiple = false; + if (in_array(MultipleAttribute::class, class_uses($element), true)) { + $multiple = $element->isMultiple(); + } + + // TODO: I fear that nested fieldsets suffer from the same issue + /** * We don't change the {@see BaseFormElement::$name} property of the element, * otherwise methods like {@see FormElements::populate() and {@see FormElements::getElement()} would fail, * but only change the name attribute to nest the names. */ return sprintf( - '%s[%s]', + '%s[%s]%s', $this->getValueOfNameAttribute(), - $element->getName() + $element->getName(), + $multiple ? '[]' : '' ); }); }