diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 2b69ac0..3a9795a 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -565,6 +565,7 @@ class FormBuilder * @param string $selected * @param array $selectAttributes * @param array $optionsAttributes + * @param array $optgroupsAttributes * * @return \Illuminate\Support\HtmlString */ @@ -573,7 +574,8 @@ class FormBuilder $list = [], $selected = null, array $selectAttributes = [], - array $optionsAttributes = [] + array $optionsAttributes = [], + array $optgroupsAttributes = [] ) { $this->type = 'select'; @@ -599,8 +601,9 @@ class FormBuilder } foreach ($list as $value => $display) { - $optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : []; - $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes); + $optionAttributes = $optionsAttributes[$value] ?? []; + $optgroupAttributes = $optgroupsAttributes[$value] ?? []; + $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes); } // Once we have all of this HTML, we can join this into a single element after @@ -675,13 +678,14 @@ class FormBuilder * @param string $value * @param string $selected * @param array $attributes + * @param array $optgroupAttributes * * @return \Illuminate\Support\HtmlString */ - public function getSelectOption($display, $value, $selected, array $attributes = []) + public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = []) { if (is_array($display)) { - return $this->optionGroup($display, $value, $selected, $attributes); + return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes); } return $this->option($display, $value, $selected, $attributes); @@ -694,18 +698,21 @@ class FormBuilder * @param string $label * @param string $selected * @param array $attributes + * @param array $optionsAttributes * * @return \Illuminate\Support\HtmlString */ - protected function optionGroup($list, $label, $selected, array $attributes = []) + protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = []) { $html = []; foreach ($list as $value => $display) { - $html[] = $this->option($display, $value, $selected, $attributes); - } + $optionAttributes = $optionsAttributes[$value] ?? []; - return $this->toHtmlString('' . implode('', $html) . ''); + $html[] = $this->option($display, $value, $selected, $optionAttributes); + } + + return $this->toHtmlString('html->attributes($attributes) . '>' . implode('', $html) . ''); } /** @@ -724,7 +731,12 @@ class FormBuilder $options = ['value' => $value, 'selected' => $selected] + $attributes; - return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); + $string = 'html->attributes($options) . '>'; + if ($display !== null) { + $string .= e($display) . ''; + } + + return $this->toHtmlString($string); } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index d7b1a95..e6b1765 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -402,6 +402,37 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase '' ); + $select = $this->formBuilder->select( + 'size', + [ + 'Large sizes' => [ + 'L' => 'Large', + 'XL' => 'Extra Large', + ], + 'M' => 'Medium', + 'Small sizes' => [ + 'S' => 'Small', + 'XS' => 'Extra Small', + ], + ], + null, + [], + [ + 'Large sizes' => [ + 'L' => ['disabled'] + ], + 'M' => ['disabled'], + ], + [ + 'Small sizes' => ['disabled'], + ] + ); + + $this->assertEquals( + $select, + '' + ); + $select = $this->formBuilder->select( 'encoded_html', ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'],