From 818efc2e160aa56e8475219faed24542e80ee25d Mon Sep 17 00:00:00 2001 From: Tortue Torche Date: Thu, 8 Feb 2018 19:38:44 +0100 Subject: [PATCH 01/10] [5.6] Form method of hidden input '_method' is preserved on next request (#486) * Form method of hidden input '_method' is preserved on next request E.G. When you submitted data across a form with a 'PATCH' method and then in a second submit you use a form with a 'DELETE' method, the FormBuilder::getValueAttribute() method use the previous value of the hidden input '_method'. So the value is replaced by 'PATCH' instead of the 'DELETE' value. * Drop PHP 7.1 and add PHP 7.2 in Travis CI config Because Laravel 5.6 require at least PHP 7.1.3 * Fix PHP 7.2 Warning with count() Otherwise a PHP Warning is thrown: count(): Parameter must be an array or an object that implements Countable See http://php.net/manual/en/migration72.incompatible.php#migration72.incompatible.warn-on-non-countable-types * Bump Mockery version to support PHP 7.2 --- .travis.yml | 1 + composer.json | 2 +- src/FormBuilder.php | 4 ++-- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index caba7c8..ccffcd9 100755 --- a/.travis.yml +++ b/.travis.yml @@ -2,6 +2,7 @@ language: php php: - 7.1 + - 7.2 sudo: false diff --git a/composer.json b/composer.json index 6012ec0..119fce8 100755 --- a/composer.json +++ b/composer.json @@ -27,7 +27,7 @@ }, "require-dev": { "illuminate/database": "5.6.*", - "mockery/mockery": "~0.9.4", + "mockery/mockery": "~1.0", "phpunit/phpunit": "~5.4" }, "autoload": { diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 01a8d88..3cfc645 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -1184,7 +1184,7 @@ class FormBuilder } $request = $this->request($name); - if (! is_null($request)) { + if (! is_null($request) && $name != '_method') { return $request; } @@ -1268,7 +1268,7 @@ class FormBuilder */ public function oldInputIsEmpty() { - return (isset($this->session) && count($this->session->getOldInput()) === 0); + return (isset($this->session) && count((array) $this->session->getOldInput()) === 0); } /** From 41cd9291a69bd24f2184e504be041348a87308a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Anderson=20Fria=C3=A7a?= Date: Mon, 12 Feb 2018 11:19:42 -0300 Subject: [PATCH 02/10] [5.6] Validation in select field for boolean value of selected option (#491) --- src/FormBuilder.php | 6 ++++-- tests/FormBuilderTest.php | 6 ++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 3cfc645..c701de9 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -572,7 +572,7 @@ class FormBuilder * * @param string $name * @param array $list - * @param string $selected + * @param string|bool $selected * @param array $selectAttributes * @param array $optionsAttributes * @param array $optgroupsAttributes @@ -787,7 +787,9 @@ class FormBuilder } elseif ($selected instanceof Collection) { return $selected->contains($value) ? 'selected' : null; } - + if (is_int($value) && is_bool($selected)) { + return (bool)$value === $selected; + } return ((string) $value === (string) $selected) ? 'selected' : null; } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 47ff1b9..dc98c81 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -464,6 +464,12 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase '', $result ); + + $select = $this->formBuilder->select('avc', [1 => 'Yes', 0 => 'No'], true, ['placeholder' => 'Select']); + $this->assertEquals( + '', + $select + ); } public function testSelectCollection() From 5060a6641dbdf72ea277e30f353931da2617c00a Mon Sep 17 00:00:00 2001 From: Devin Date: Fri, 16 Mar 2018 09:57:16 -0700 Subject: [PATCH 03/10] Fixed bug with radio button not being checked (#501) * Fixed bug with radio button not being checked Fixes: https://github.com/LaravelCollective/html/issues/496 Loose comparison needs to be done because Laravel model casting may be in affect and therfore 1 == true and 0 == false. * typo --- src/FormBuilder.php | 20 +++++++++++++++++--- tests/FormBuilderTest.php | 15 +++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index c701de9..bdfa63f 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -190,7 +190,7 @@ class FormBuilder { $this->model = $model; } - + /** * Get the current model instance on the form builder. * @@ -871,7 +871,7 @@ class FormBuilder return $this->getRadioCheckedState($name, $value, $checked); default: - return $this->getValueAttribute($name) === $value; + return $this->compareValues($name, $value); } } @@ -924,7 +924,21 @@ class FormBuilder return $checked; } - return $this->getValueAttribute($name) === $value; + return $this->compareValues($name, $value); + } + + /** + * Determine if the provide value loosely compares to the value assigned to the field. + * Use loose comparison because Laravel model casting may be in affect and therefore + * 1 == true and 0 == false. + * + * @param string $name + * @param string $value + * @return bool + */ + protected function compareValues($name, $value) + { + return $this->getValueAttribute($name) == $value; } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index dc98c81..4a846cb 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -658,6 +658,21 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $this->assertEquals('', $form4); } + public function testFormRadioWithAttributeCastToBoolean() + { + $this->setModel(['itemA' => true, 'itemB' => false]); + + $radio1 = $this->formBuilder->radio('itemA', 1); + $radio2 = $this->formBuilder->radio('itemA', 0); + $radio3 = $this->formBuilder->radio('itemB', 1); + $radio4 = $this->formBuilder->radio('itemB', 0); + + $this->assertEquals('', $radio1); + $this->assertEquals('', $radio2); + $this->assertEquals('', $radio3); + $this->assertEquals('', $radio4); + } + public function testFormRadioRepopulation() { $this->formBuilder->setSessionStore($session = m::mock('Illuminate\Contracts\Session\Session')); From 623a150c91e2d3f92eeee9f9eda58a841e3cb548 Mon Sep 17 00:00:00 2001 From: ilyadrv Date: Fri, 16 Mar 2018 17:57:31 +0100 Subject: [PATCH 04/10] Placeholder for select element not hidden anymore (#493) (#499) * Placeholder for select element not hidden anymore (#493) * Fix tests for placeholder select element without hidden attribue --- src/FormBuilder.php | 2 +- tests/FormBuilderTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index bdfa63f..2d7a214 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -769,7 +769,7 @@ class FormBuilder 'value' => '', ]; - return $this->toHtmlString('html->attributes($options) . ' hidden="hidden">' . e($display, false) . ''); + return $this->toHtmlString('html->attributes($options) . '>' . e($display, false) . ''); } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 4a846cb..ced7c3b 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -467,7 +467,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase $select = $this->formBuilder->select('avc', [1 => 'Yes', 0 => 'No'], true, ['placeholder' => 'Select']); $this->assertEquals( - '', + '', $select ); } @@ -516,7 +516,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase ['placeholder' => 'Select One...'] ); $this->assertEquals($select, - ''); + ''); $select = $this->formBuilder->select( 'size', @@ -525,7 +525,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase ['placeholder' => 'Select One...'] ); $this->assertEquals($select, - ''); + ''); $select = $this->formBuilder->select( 'encoded_html', @@ -534,7 +534,7 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase ['placeholder' => 'Select the  '] ); $this->assertEquals($select, - '' + '' ); } From b3a10245c791a211e5f8ec37117f4549cd22aabe Mon Sep 17 00:00:00 2001 From: Adrian Crisan Date: Mon, 9 Apr 2018 17:09:32 +0300 Subject: [PATCH 05/10] Fix the issue where defaults has a higher priority then the attributes (#509) --- src/HtmlBuilder.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index ec95354..17da19f 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -94,7 +94,7 @@ class HtmlBuilder { $defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet']; - $attributes = array_merge($attributes, $defaults); + $attributes = array_merge($defaults, $attributes); $attributes['href'] = $this->url->asset($url, $secure); From 38f2c6ff0d3dfc497175a5cff5e24b3937f6e2c9 Mon Sep 17 00:00:00 2001 From: Gabriel Caruso Date: Thu, 26 Apr 2018 13:09:18 -0300 Subject: [PATCH 06/10] Update to PHPUnit 7.1 (#511) --- composer.json | 2 +- tests/FormAccessibleTest.php | 6 +++--- tests/FormBuilderTest.php | 2 +- tests/HtmlBuilderTest.php | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index 119fce8..72a47c1 100755 --- a/composer.json +++ b/composer.json @@ -28,7 +28,7 @@ "require-dev": { "illuminate/database": "5.6.*", "mockery/mockery": "~1.0", - "phpunit/phpunit": "~5.4" + "phpunit/phpunit": "~7.1" }, "autoload": { "psr-4": { diff --git a/tests/FormAccessibleTest.php b/tests/FormAccessibleTest.php index 6a73728..3471d1e 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -12,7 +12,7 @@ use Illuminate\Routing\UrlGenerator; use Illuminate\Database\Capsule\Manager as Capsule; use Mockery as m; -class FormAccessibleTest extends PHPUnit_Framework_TestCase +class FormAccessibleTest extends PHPUnit\Framework\TestCase { public function setUp() { @@ -74,12 +74,12 @@ class FormAccessibleTest extends PHPUnit_Framework_TestCase $model = new ModelThatUsesForms($this->modelData); $this->assertEquals($model->getFormValue('address.street'), 'abcde st'); } - + public function testItCanUseGetAccessorValuesWhenThereAreNoFormAccessors() { $model = new ModelThatUsesForms($this->modelData); $this->formBuilder->setModel($model); - + $this->assertEquals($this->formBuilder->getValueAttribute('email'), 'mutated@tjshafer.com'); } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index ced7c3b..5e15afd 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -11,7 +11,7 @@ use Illuminate\Support\Facades\App; use Illuminate\Session\Store; use Mockery as m; -class FormBuilderTest extends PHPUnit_Framework_TestCase +class FormBuilderTest extends PHPUnit\Framework\TestCase { /** * @var FormBuilder diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index 3cb6029..a5a9ee2 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -7,7 +7,7 @@ use Illuminate\Routing\RouteCollection; use Illuminate\Routing\UrlGenerator; use Mockery as m; -class HtmlBuilderTest extends PHPUnit_Framework_TestCase +class HtmlBuilderTest extends PHPUnit\Framework\TestCase { /** From 42c0854e00d6bb3346883d122b444fbf15a13ecb Mon Sep 17 00:00:00 2001 From: Drew <5396418+djh101@users.noreply.github.com> Date: Thu, 10 May 2018 10:15:15 -0700 Subject: [PATCH 07/10] Add range, month, week. (#520) * Added range, month, week. * Added URL test. * Removed duplicate function testFormFile. --- src/FormBuilder.php | 54 +++++++++++++++++++++++++++++++++++++++ tests/FormBuilderTest.php | 46 +++++++++++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 2d7a214..ef91817 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -329,6 +329,20 @@ class FormBuilder return $this->input('password', $name, '', $options); } + /** + * Create a range input field. + * + * @param string $name + * @param string $value + * @param array $options + * + * @return \Illuminate\Support\HtmlString + */ + public function range($name, $value = null, $options = []) + { + return $this->input('range', $name, $value, $options); + } + /** * Create a hidden input field. * @@ -464,6 +478,10 @@ class FormBuilder */ public function time($name, $value = null, $options = []) { + if ($value instanceof DateTime) { + $value = $value->format('H:i'); + } + return $this->input('time', $name, $value, $options); } @@ -481,6 +499,24 @@ class FormBuilder return $this->input('url', $name, $value, $options); } + /** + * Create a week input field. + * + * @param string $name + * @param string $value + * @param array $options + * + * @return \Illuminate\Support\HtmlString + */ + public function week($name, $value = null, $options = []) + { + if ($value instanceof DateTime) { + $value = $value->format('Y-\WW'); + } + + return $this->input('week', $name, $value, $options); + } + /** * Create a file input field. * @@ -982,6 +1018,24 @@ class FormBuilder return $this->input('image', $name, null, $attributes); } + /** + * Create a month input field. + * + * @param string $name + * @param string $value + * @param array $options + * + * @return \Illuminate\Support\HtmlString + */ + public function month($name, $value = null, $options = []) + { + if ($value instanceof DateTime) { + $value = $value->format('Y-m'); + } + + return $this->input('month', $name, $value, $options); + } + /** * Create a color input field. * diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 5e15afd..e53f007 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -229,6 +229,17 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase $this->assertEquals('', $form2); } + public function testFormRange() + { + $form1 = $this->formBuilder->range('foo'); + $form2 = $this->formBuilder->range('foo', 1); + $form3 = $this->formBuilder->range('foo', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } + public function testFormHidden() { $form1 = $this->formBuilder->hidden('foo'); @@ -240,6 +251,18 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase $this->assertEquals('', $form3); } + public function testFormMonth() + { + $form1 = $this->formBuilder->month('foo'); + $form2 = $this->formBuilder->month('foo', \Carbon\Carbon::now()); + $form3 = $this->formBuilder->month('foo', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', + $form2); + $this->assertEquals('', $form3); + } + public function testFormSearch() { $form1 = $this->formBuilder->search('foo'); @@ -310,6 +333,29 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase $this->assertEquals('', $form3); } + public function testFormUrl() + { + $form1 = $this->formBuilder->url('foo'); + $form2 = $this->formBuilder->url('foo', 'http://foobar.com'); + $form3 = $this->formBuilder->url('foo', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + } + + public function testFormWeek() + { + $form1 = $this->formBuilder->week('foo'); + $form2 = $this->formBuilder->week('foo', \Carbon\Carbon::now()); + $form3 = $this->formBuilder->week('foo', null, ['class' => 'span2']); + + $this->assertEquals('', $form1); + $this->assertEquals('', + $form2); + $this->assertEquals('', $form3); + } + public function testFormFile() { $form1 = $this->formBuilder->file('foo'); From 6d1ccebb53ca4ae3d56ead949a39a7eb851ca16b Mon Sep 17 00:00:00 2001 From: Victor Isadov Date: Wed, 30 May 2018 19:07:28 +0300 Subject: [PATCH 08/10] Make Request optional (#522) * Make Request optional * StyleCI --- src/FormBuilder.php | 32 ++++++++++++++++++++++++++------ tests/FormBuilderTest.php | 26 +++++++++++++++++--------- 2 files changed, 43 insertions(+), 15 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index ef91817..a1f1da6 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -49,6 +49,12 @@ class FormBuilder */ protected $csrfToken; + /** + * Consider Request variables while auto fill. + * @var bool + */ + protected $considerRequest = false; + /** * The session store implementation. * @@ -108,6 +114,7 @@ class FormBuilder * @param \Illuminate\Contracts\Routing\UrlGenerator $url * @param \Illuminate\Contracts\View\Factory $view * @param string $csrfToken + * @param Request $request */ public function __construct(HtmlBuilder $html, UrlGenerator $url, Factory $view, $csrfToken, Request $request = null) { @@ -1246,7 +1253,7 @@ class FormBuilder if ($hasNullMiddleware && is_null($old) && is_null($value) - && ! is_null($this->view->shared('errors')) + && !is_null($this->view->shared('errors')) && count($this->view->shared('errors')) > 0 ) { return null; @@ -1267,6 +1274,15 @@ class FormBuilder } } + /** + * Take Request in fill process + * @param bool $consider + */ + public function considerRequest($consider = true) + { + $this->considerRequest = $consider; + } + /** * Get value from current Request * @param $name @@ -1274,7 +1290,11 @@ class FormBuilder */ protected function request($name) { - if (! isset($this->request)) { + if (!$this->considerRequest) { + return null; + } + + if (!isset($this->request)) { return null; } @@ -1312,16 +1332,16 @@ class FormBuilder $key = $this->transformKey($name); $payload = $this->session->getOldInput($key); - if (! is_array($payload)) { + if (!is_array($payload)) { return $payload; } - if (! in_array($this->type, ['select', 'checkbox'])) { - if (! isset($this->payload[$key])) { + if (!in_array($this->type, ['select', 'checkbox'])) { + if (!isset($this->payload[$key])) { $this->payload[$key] = collect($payload); } - if (! empty($this->payload[$key])) { + if (!empty($this->payload[$key])) { $value = $this->payload[$key]->shift(); return $value; } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index e53f007..6936a5f 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -31,10 +31,10 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase $request = Request::create('/foo', 'GET', [ "person" => [ "name" => "John", - "surname" => "Doe" + "surname" => "Doe", ], - "aggree" => 1, - "checkbox_array" => [1,2,3] + "agree" => 1, + "checkbox_array" => [1, 2, 3], ]); $request = Request::createFromBase($request); @@ -52,14 +52,15 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase public function testRequestValue() { - $name = $this->formBuilder->text("person[name]"); - $surname = $this->formBuilder->text("person[surname]"); + $this->formBuilder->considerRequest(); + $name = $this->formBuilder->text("person[name]", "Not John"); + $surname = $this->formBuilder->text("person[surname]", "Not Doe"); $this->assertEquals('', $name); $this->assertEquals('', $surname); - $checked = $this->formBuilder->checkbox("aggree", 1); + $checked = $this->formBuilder->checkbox("agree", 1); $unchecked = $this->formBuilder->checkbox("no_value", 1); - $this->assertEquals('', $checked); + $this->assertEquals('', $checked); $this->assertEquals('', $unchecked); $checked_array = $this->formBuilder->checkbox("checkbox_array[]", 1); @@ -67,10 +68,17 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase $this->assertEquals('', $checked_array); $this->assertEquals('', $unchecked_array); - $checked = $this->formBuilder->radio("aggree", 1); + $checked = $this->formBuilder->radio("agree", 1); $unchecked = $this->formBuilder->radio("no_value", 1); - $this->assertEquals('', $checked); + $this->assertEquals('', $checked); $this->assertEquals('', $unchecked); + + // now we check that Request is ignored and value take precedence + $this->formBuilder->considerRequest(false); + $name = $this->formBuilder->text("person[name]", "Not John"); + $surname = $this->formBuilder->text("person[surname]", "Not Doe"); + $this->assertEquals('', $name); + $this->assertEquals('', $surname); } public function testOpeningForm() From fda9d3dad85ecea609ef9c6323d6923536cf5643 Mon Sep 17 00:00:00 2001 From: ThunderbirdsX3 Date: Wed, 30 May 2018 23:09:07 +0700 Subject: [PATCH 09/10] Can set class attribute in array. (#521) In case of you want to add class by condition. ``` Form::text('name', null, ['class' => [ 'form-control-, $error ? 'is-invalid' : '', ]]); ``` --- src/HtmlBuilder.php | 4 ++++ tests/FormBuilderTest.php | 12 ++++++++++++ tests/HtmlBuilderTest.php | 14 ++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index 17da19f..743b32a 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -455,6 +455,10 @@ class HtmlBuilder return $value ? $key : ''; } + if (is_array($value) && $key === 'class') { + return 'class="' . implode(' ', $value) . '"'; + } + if (! is_null($value)) { return $key . '="' . e($value, false) . '"'; } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 6936a5f..35d43c0 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -792,6 +792,18 @@ class FormBuilderTest extends PHPUnit\Framework\TestCase $this->assertEquals('', $input); } + public function testArrayClassAttributes() + { + $input = $this->formBuilder->text('test', null, ['class' => ['class-a', 'class-b']]); + $this->assertEquals('', $input); + + $input = $this->formBuilder->text('test', null, ['class' => [ + 'class-a', + false ? 'class-b' : 'class-c' + ]]); + $this->assertEquals('', $input); + } + protected function setModel(array $data, $object = true) { if ($object) { diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index a5a9ee2..f79320a 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -151,4 +151,18 @@ class HtmlBuilderTest extends PHPUnit\Framework\TestCase $this->assertEquals('', trim($result2)); } + + public function testArrayClassAttributes() + { + $result = $this->htmlBuilder->attributes(['class' => ['class-a', 'class-b']]); + + $this->assertEquals('class="class-a class-b"', trim($result)); + + $result = $this->htmlBuilder->attributes(['class' => [ + 'class-a', + false ? 'class-b' : 'class-c' + ]]); + + $this->assertEquals('class="class-a class-c"', trim($result)); + } } From 974605fcd22a7e4d19f0b2ef635a0d1d7400387d Mon Sep 17 00:00:00 2001 From: Tautvydas Date: Mon, 18 Jun 2018 18:04:16 +0300 Subject: [PATCH 10/10] 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()