Skip to content

Commit

Permalink
FieldsetElement: Correctly handle elements with multiple values
Browse files Browse the repository at this point in the history
fixes #136
  • Loading branch information
nilmerg committed Jun 12, 2024
1 parent 06a73e8 commit 9e1dfce
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/FormElement/FieldsetElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,15 +108,21 @@ 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();

Check failure on line 113 in src/FormElement/FieldsetElement.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.2 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().

Check failure on line 113 in src/FormElement/FieldsetElement.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.3 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().

Check failure on line 113 in src/FormElement/FieldsetElement.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 7.4 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().

Check failure on line 113 in src/FormElement/FieldsetElement.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.0 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().

Check failure on line 113 in src/FormElement/FieldsetElement.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.1 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().

Check failure on line 113 in src/FormElement/FieldsetElement.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.2 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().

Check failure on line 113 in src/FormElement/FieldsetElement.php

View workflow job for this annotation

GitHub Actions / phpstan / Static analysis with phpstan and php 8.3 on ubuntu-latest

Call to an undefined method ipl\Html\Contract\FormElement::isMultiple().
}

/**
* 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 ? '[]' : ''
);
});
}
Expand Down
50 changes: 50 additions & 0 deletions tests/FormElement/FieldsetElementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,4 +270,54 @@ public function testNestedFieldsetsAlsoOnlyHaveAValueIfOneOfTheirElementsHaveOne
'Nested fieldsets with non-empty elements are not non-empty themselves'
);
}

public function testMultiSelectionFieldInFieldset(): void
{
$fieldset = (new FieldsetElement('test_fieldset'))
->addElement('select', 'test_select', [
'multiple' => true,
'options' => [
'one' => 'One',
'two' => 'Two'
]
]);

$expected = <<<'HTML'
<fieldset name="test_fieldset">
<select multiple="multiple" name="test_fieldset[test_select][]">
<option value="one">One</option>
<option value="two">Two</option>
</select>
</fieldset>
HTML;

$this->assertHtml($expected, $fieldset);
}

public function testMultiSelectionFieldInNestedFieldset(): void
{
$inner = (new FieldsetElement('inner'))
->addElement('select', 'test_select', [
'multiple' => true,
'options' => [
'one' => 'One',
'two' => 'Two'
]
]);
$outer = (new FieldsetElement('outer'))
->addElement($inner);

$expected = <<<'HTML'
<fieldset name="outer">
<fieldset name="outer[inner]">
<select multiple="multiple" name="outer[inner][test_select][]">
<option value="one">One</option>
<option value="two">Two</option>
</select>
</fieldset>
</fieldset>
HTML;

$this->assertHtml($expected, $outer);
}
}

0 comments on commit 9e1dfce

Please sign in to comment.