Merge branch 'pjona-5.4-select-option-attributes' into 5.4

This commit is contained in:
mlantz committed 2017-05-14 18:27:49 -04:00
commit 204f3eb3d4
2 files changed
+64 -28

No files matched your search

+42 -28
View File
@@ -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("<select{$options}>{$list}</select>");
return $this->toHtmlString("<select{$selectAttributes}>{$list}</select>");
}
/**
@@ -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('<optgroup label="' . e($label) . '">' . implode('', $html) . '</optgroup>');
@@ -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('<option' . $this->html->attributes($options) . '>' . e($display) . '</option>');
}
@@ -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)) {
+22
View File
@@ -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 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>'
);
$select = $this->formBuilder->select(
'encoded_html',
['no_break_space' => '&nbsp;', 'ampersand' => '&amp;', 'lower_than' => '&lt;'],
null
);
$this->assertEquals(
$select,
'<select name="encoded_html"><option value="no_break_space">&nbsp;</option><option value="ampersand">&amp;</option><option value="lower_than">&lt;</option></select>'
);
$select = $this->formBuilder->select(
'size',
['L' => 'Large', 'S' => 'Small'],
null,
[],
['L' => ['data-foo' => 'bar', 'disabled']]
);
$this->assertEquals($select,
'<select name="size"><option value="L" data-foo="bar" disabled="disabled">Large</option><option value="S">Small</option></select>');
}
public function testFormSelectRepopulation()