diff --git a/src/FormBuilder.php b/src/FormBuilder.php index cff42d5..ca5230c 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -2,14 +2,15 @@ namespace Collective\Html; -use DateTime; use BadMethodCallException; -use Illuminate\Support\Collection; -use Illuminate\Support\HtmlString; -use Illuminate\Contracts\View\Factory; -use Illuminate\Contracts\Session\Session; -use Illuminate\Support\Traits\Macroable; +use DateTime; use Illuminate\Contracts\Routing\UrlGenerator; +use Illuminate\Contracts\Session\Session; +use Illuminate\Contracts\View\Factory; +use Illuminate\Support\Collection; +use Illuminate\Support\Facades\App; +use Illuminate\Support\HtmlString; +use Illuminate\Support\Traits\Macroable; class FormBuilder { @@ -531,21 +532,28 @@ class FormBuilder * @param string $name * @param array $list * @param string $selected - * @param array $options + * @param array $selectAttributes + * @param array $optionsAttributes * * @return \Illuminate\Support\HtmlString */ - public function select($name, $list = [], $selected = null, $options = []) + public function select( + $name, + array $list = [], + $selected = null, + array $selectAttributes = [], + array $optionsAttributes = [] + ) { // When building a select box the "value" attribute is really the selected one // so we will use that when checking the model or session for a value which // should provide a convenient method of re-populating the forms on post. $selected = $this->getValueAttribute($name, $selected); - $options['id'] = $this->getIdAttribute($name, $options); + $selectAttributes['id'] = $this->getIdAttribute($name, $selectAttributes); - if (! isset($options['name'])) { - $options['name'] = $name; + if (! isset($selectAttributes['name'])) { + $selectAttributes['name'] = $name; } // We will simply loop through the options and build an HTML value for each of @@ -553,23 +561,24 @@ class FormBuilder // all together into one single HTML element that can be put on the form. $html = []; - if (isset($options['placeholder'])) { - $html[] = $this->placeholderOption($options['placeholder'], $selected); - unset($options['placeholder']); + if (isset($selectAttributes['placeholder'])) { + $html[] = $this->placeholderOption($selectAttributes['placeholder'], $selected); + unset($selectAttributes['placeholder']); } foreach ($list as $value => $display) { - $html[] = $this->getSelectOption($display, $value, $selected); + $optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : []; + $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes); } // Once we have all of this HTML, we can join this into a single element after // formatting the attributes into an HTML "attributes" string, then we will // build out a final select statement, which will contain all the values. - $options = $this->html->attributes($options); + $selectAttributes = $this->html->attributes($selectAttributes); $list = implode('', $html); - return $this->toHtmlString("{$list}"); + return $this->toHtmlString("{$list}"); } /** @@ -633,16 +642,17 @@ class FormBuilder * @param string $display * @param string $value * @param string $selected + * @param array $attributes * * @return \Illuminate\Support\HtmlString */ - public function getSelectOption($display, $value, $selected) + public function getSelectOption($display, $value, $selected, array $attributes = []) { if (is_array($display)) { - return $this->optionGroup($display, $value, $selected); + return $this->optionGroup($display, $value, $selected, $attributes); } - return $this->option($display, $value, $selected); + return $this->option($display, $value, $selected, $attributes); } /** @@ -651,15 +661,16 @@ class FormBuilder * @param array $list * @param string $label * @param string $selected + * @param array $attributes * * @return \Illuminate\Support\HtmlString */ - protected function optionGroup($list, $label, $selected) + protected function optionGroup($list, $label, $selected, array $attributes = []) { $html = []; foreach ($list as $value => $display) { - $html[] = $this->option($display, $value, $selected); + $html[] = $this->option($display, $value, $selected, $attributes); } return $this->toHtmlString('' . implode('', $html) . ''); @@ -671,14 +682,15 @@ class FormBuilder * @param string $display * @param string $value * @param string $selected + * @param array $attributes * * @return \Illuminate\Support\HtmlString */ - protected function option($display, $value, $selected) + protected function option($display, $value, $selected, array $attributes = []) { $selected = $this->getSelectedValue($value, $selected); - $options = ['value' => $value, 'selected' => $selected]; + $options = ['value' => $value, 'selected' => $selected] + $attributes; return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); } @@ -1088,11 +1100,13 @@ class FormBuilder return $this->old($name); } - $hasNullMiddleware = app("Illuminate\Contracts\Http\Kernel") - ->hasMiddleware('Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull'); + if (function_exists('app')) { + $hasNullMiddleware = app("Illuminate\Contracts\Http\Kernel") + ->hasMiddleware('Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull'); - if ($hasNullMiddleware && is_null($value)) { - return null; + if ($hasNullMiddleware && is_null($value)) { + return null; + } } if (! is_null($value)) { diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index d117158..f97b127 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -7,6 +7,7 @@ use Illuminate\Http\Request; use Illuminate\Routing\RouteCollection; use Illuminate\Routing\UrlGenerator; use Illuminate\Support\Collection; +use Illuminate\Support\Facades\App; use Mockery as m; class FormBuilderTest extends PHPUnit_Framework_TestCase @@ -305,6 +306,27 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $select, '' ); + + $select = $this->formBuilder->select( + 'encoded_html', + ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'], + null + ); + + $this->assertEquals( + $select, + '' + ); + + $select = $this->formBuilder->select( + 'size', + ['L' => 'Large', 'S' => 'Small'], + null, + [], + ['L' => ['data-foo' => 'bar', 'disabled']] + ); + $this->assertEquals($select, + ''); } public function testFormSelectRepopulation()