Skip to content

Commit 974605f

Browse files
tautvydasrtshafer
authored andcommitted
Allow sub-select to be iterable to accept array and collection (#540)
1 parent fda9d3d commit 974605f

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

src/FormBuilder.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,7 @@ public function selectMonth($name, $selected = null, $options = [], $format = '%
737737
*/
738738
public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = [])
739739
{
740-
if (is_array($display)) {
740+
if (is_iterable($display)) {
741741
return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes);
742742
}
743743

@@ -762,7 +762,7 @@ protected function optionGroup($list, $label, $selected, array $attributes = [],
762762
$space = str_repeat(" ", $level);
763763
foreach ($list as $value => $display) {
764764
$optionAttributes = $optionsAttributes[$value] ?? [];
765-
if (is_array($display)) {
765+
if (is_iterable($display)) {
766766
$html[] = $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level+5);
767767
} else {
768768
$html[] = $this->option($space.$display, $value, $selected, $optionAttributes);

tests/FormBuilderTest.php

+52
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,58 @@ public function testSelectCollection()
537537
);
538538
$this->assertEquals($select,
539539
'<select name="size"><option value="L" data-foo="bar" disabled>Large</option><option value="S">Small</option></select>');
540+
541+
$select = $this->formBuilder->select(
542+
'size',
543+
collect([
544+
'Large sizes' => collect([
545+
'L' => 'Large',
546+
'XL' => 'Extra Large',
547+
]),
548+
'S' => 'Small',
549+
]),
550+
null,
551+
[
552+
'class' => 'class-name',
553+
'id' => 'select-id',
554+
]
555+
);
556+
557+
$this->assertEquals(
558+
$select,
559+
'<select class="class-name" id="select-id" name="size"><optgroup label="Large sizes"><option value="L">Large</option><option value="XL">Extra Large</option></optgroup><option value="S">Small</option></select>'
560+
);
561+
562+
$select = $this->formBuilder->select(
563+
'size',
564+
collect([
565+
'Large sizes' => collect([
566+
'L' => 'Large',
567+
'XL' => 'Extra Large',
568+
]),
569+
'M' => 'Medium',
570+
'Small sizes' => collect([
571+
'S' => 'Small',
572+
'XS' => 'Extra Small',
573+
]),
574+
]),
575+
null,
576+
[],
577+
[
578+
'Large sizes' => [
579+
'L' => ['disabled']
580+
],
581+
'M' => ['disabled'],
582+
],
583+
[
584+
'Small sizes' => ['disabled'],
585+
]
586+
);
587+
588+
$this->assertEquals(
589+
$select,
590+
'<select name="size"><optgroup label="Large sizes"><option value="L" disabled>Large</option><option value="XL">Extra Large</option></optgroup><option value="M" disabled>Medium</option><optgroup label="Small sizes" disabled><option value="S">Small</option><option value="XS">Extra Small</option></optgroup></select>'
591+
);
540592
}
541593

542594
public function testFormSelectRepopulation()

0 commit comments

Comments
 (0)