Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FieldsetElement: Correctly handle elements with multiple values #137

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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);
}
}
Loading