From 974605fcd22a7e4d19f0b2ef635a0d1d7400387d Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Mon, 18 Jun 2018 18:04:16 +0300 Subject: [PATCH] Allow sub-select to be iterable to accept array and collection (#540) --- src/FormBuilder.php | 4 +-- tests/FormBuilderTest.php | 52 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index a1f1da6..c15ef66 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -737,7 +737,7 @@ class FormBuilder */ public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = []) { - if (is_array($display)) { + if (is_iterable($display)) { return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes); } @@ -762,7 +762,7 @@ class FormBuilder $space = str_repeat(" ", $level); foreach ($list as $value => $display) { $optionAttributes = $optionsAttributes[$value] ?? []; - if (is_array($display)) { + if (is_iterable($display)) { $html[] = $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level+5); } else { $html[] = $this->option($space.$display, $value, $selected, $optionAttributes); diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 35d43c0..b343cc1 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -537,6 +537,58 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase ); $this->assertEquals($select, ''); + + $select = $this->formBuilder->select( + 'size', + collect([ + 'Large sizes' => collect([ + 'L' => 'Large', + 'XL' => 'Extra Large', + ]), + 'S' => 'Small', + ]), + null, + [ + 'class' => 'class-name', + 'id' => 'select-id', + ] + ); + + $this->assertEquals( + $select, + '' + ); + + $select = $this->formBuilder->select( + 'size', + collect([ + 'Large sizes' => collect([ + 'L' => 'Large', + 'XL' => 'Extra Large', + ]), + 'M' => 'Medium', + 'Small sizes' => collect([ + 'S' => 'Small', + 'XS' => 'Extra Small', + ]), + ]), + null, + [], + [ + 'Large sizes' => [ + 'L' => ['disabled'] + ], + 'M' => ['disabled'], + ], + [ + 'Small sizes' => ['disabled'], + ] + ); + + $this->assertEquals( + $select, + '' + ); } public function testFormSelectRepopulation()