From cecb65626c33e89d9f8545d3119b5b7d0119b96f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Llu=C3=ADs=20Bayer=20Soler?= Date: Mon, 5 Feb 2018 15:04:30 +0100 Subject: [PATCH] Nested optgroups (#470) * fix optionGroup and added a function to handle recursively the arrays * simplify recursively * fix codestyle * fix codestyle * add new parameter annotation --- src/FormBuilder.php | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index a0cb439..dd2753a 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -699,20 +699,23 @@ class FormBuilder * @param string $selected * @param array $attributes * @param array $optionsAttributes + * @param integer $level * * @return \Illuminate\Support\HtmlString */ - protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = []) + protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = [], $level = 0) { $html = []; - + $space = str_repeat(" ", $level); foreach ($list as $value => $display) { $optionAttributes = $optionsAttributes[$value] ?? []; - - $html[] = $this->option($display, $value, $selected, $optionAttributes); + if (is_array($display)) { + $html[] = $this->optionGroup($display, $value, $selected, $attributes, $optionAttributes, $level+5); + } else { + $html[] = $this->option($space.$display, $value, $selected, $attributes, $optionAttributes); + } } - - return $this->toHtmlString('html->attributes($attributes) . '>' . implode('', $html) . ''); + return $this->toHtmlString('' . implode('', $html) . ''); } /**