From 818efc2e160aa56e8475219faed24542e80ee25d Mon Sep 17 00:00:00 2001 From: Tortue Torche <tortuetorche@users.noreply.github.com> 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 caba7c82..ccffcd93 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 6012ec0b..119fce88 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 01a8d88f..3cfc6458 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -1184,7 +1184,7 @@ public function getValueAttribute($name, $value = null) } $request = $this->request($name); - if (! is_null($request)) { + if (! is_null($request) && $name != '_method') { return $request; } @@ -1268,7 +1268,7 @@ public function old($name) */ 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?= <afriacaxd@gmail.com> 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 3cfc6458..c701de9c 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -572,7 +572,7 @@ protected function setQuickTextAreaSize($options) * * @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 @@ protected function getSelectedValue($value, $selected) } 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 47ff1b92..dc98c81e 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -464,6 +464,12 @@ public function testSelect() '<select name="countries"><option value="1" selected="selected">L</option><option value="2">M</option></select>', $result ); + + $select = $this->formBuilder->select('avc', [1 => 'Yes', 0 => 'No'], true, ['placeholder' => 'Select']); + $this->assertEquals( + '<select name="avc"><option value="" hidden="hidden">Select</option><option value="1" selected>Yes</option><option value="0" >No</option></select>', + $select + ); } public function testSelectCollection() From 5060a6641dbdf72ea277e30f353931da2617c00a Mon Sep 17 00:00:00 2001 From: Devin <dddesigns3@gmail.com> 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 c701de9c..bdfa63fc 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -190,7 +190,7 @@ public function setModel($model) { $this->model = $model; } - + /** * Get the current model instance on the form builder. * @@ -871,7 +871,7 @@ protected function getCheckedState($type, $name, $value, $checked) return $this->getRadioCheckedState($name, $value, $checked); default: - return $this->getValueAttribute($name) === $value; + return $this->compareValues($name, $value); } } @@ -924,7 +924,21 @@ protected function getRadioCheckedState($name, $value, $checked) 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 dc98c81e..4a846cb3 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -658,6 +658,21 @@ public function testFormRadio() $this->assertEquals('<input class="span2" name="foo" type="radio" value="foobar">', $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('<input checked="checked" name="itemA" type="radio" value="1">', $radio1); + $this->assertEquals('<input name="itemA" type="radio" value="0">', $radio2); + $this->assertEquals('<input name="itemB" type="radio" value="1">', $radio3); + $this->assertEquals('<input checked="checked" name="itemB" type="radio" value="0">', $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 <ilya.drv@gmail.com> 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 bdfa63fc..2d7a2144 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -769,7 +769,7 @@ protected function placeholderOption($display, $selected) 'value' => '', ]; - return $this->toHtmlString('<option' . $this->html->attributes($options) . ' hidden="hidden">' . e($display, false) . '</option>'); + return $this->toHtmlString('<option' . $this->html->attributes($options) . '>' . e($display, false) . '</option>'); } /** diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 4a846cb3..ced7c3b7 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -467,7 +467,7 @@ public function testSelect() $select = $this->formBuilder->select('avc', [1 => 'Yes', 0 => 'No'], true, ['placeholder' => 'Select']); $this->assertEquals( - '<select name="avc"><option value="" hidden="hidden">Select</option><option value="1" selected>Yes</option><option value="0" >No</option></select>', + '<select name="avc"><option value="">Select</option><option value="1" selected>Yes</option><option value="0" >No</option></select>', $select ); } @@ -516,7 +516,7 @@ public function testFormWithOptionalPlaceholder() ['placeholder' => 'Select One...'] ); $this->assertEquals($select, - '<select name="size"><option selected="selected" value="" hidden="hidden">Select One...</option><option value="L">Large</option><option value="S">Small</option></select>'); + '<select name="size"><option selected="selected" value="">Select One...</option><option value="L">Large</option><option value="S">Small</option></select>'); $select = $this->formBuilder->select( 'size', @@ -525,7 +525,7 @@ public function testFormWithOptionalPlaceholder() ['placeholder' => 'Select One...'] ); $this->assertEquals($select, - '<select name="size"><option value="" hidden="hidden">Select One...</option><option value="L" selected="selected">Large</option><option value="S">Small</option></select>'); + '<select name="size"><option value="">Select One...</option><option value="L" selected="selected">Large</option><option value="S">Small</option></select>'); $select = $this->formBuilder->select( 'encoded_html', @@ -534,7 +534,7 @@ public function testFormWithOptionalPlaceholder() ['placeholder' => 'Select the '] ); $this->assertEquals($select, - '<select name="encoded_html"><option selected="selected" value="" hidden="hidden">Select the </option><option value="no_break_space"> </option><option value="ampersand">&</option><option value="lower_than"><</option></select>' + '<select name="encoded_html"><option selected="selected" value="">Select the </option><option value="no_break_space"> </option><option value="ampersand">&</option><option value="lower_than"><</option></select>' ); } From b3a10245c791a211e5f8ec37117f4549cd22aabe Mon Sep 17 00:00:00 2001 From: Adrian Crisan <adrian.crisan88@gmail.com> 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 ec953546..17da19f5 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -94,7 +94,7 @@ public function style($url, $attributes = [], $secure = null) { $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 <carusogabriel34@gmail.com> 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 119fce88..72a47c15 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 6a737280..3471d1e8 100644 --- a/tests/FormAccessibleTest.php +++ b/tests/FormAccessibleTest.php @@ -12,7 +12,7 @@ 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 @@ public function testItCanGetRelatedValueForForms() $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 ced7c3b7..5e15afd9 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -11,7 +11,7 @@ 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 3cb6029f..a5a9ee27 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -7,7 +7,7 @@ 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 2d7a2144..ef918176 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -329,6 +329,20 @@ public function password($name, $options = []) 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 @@ public function datetimeLocal($name, $value = null, $options = []) */ 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 @@ public function url($name, $value = null, $options = []) 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 @@ public function image($url, $name = null, $attributes = []) 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 5e15afd9..e53f0072 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -229,6 +229,17 @@ public function testFormPassword() $this->assertEquals('<input class="span2" name="foo" type="password" value="">', $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('<input name="foo" type="range">', $form1); + $this->assertEquals('<input name="foo" type="range" value="1">', $form2); + $this->assertEquals('<input class="span2" name="foo" type="range">', $form3); + } + public function testFormHidden() { $form1 = $this->formBuilder->hidden('foo'); @@ -240,6 +251,18 @@ public function testFormHidden() $this->assertEquals('<input class="span2" name="foo" type="hidden">', $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('<input name="foo" type="month">', $form1); + $this->assertEquals('<input name="foo" type="month" value="' . \Carbon\Carbon::now()->format('Y-m') . '">', + $form2); + $this->assertEquals('<input class="span2" name="foo" type="month">', $form3); + } + public function testFormSearch() { $form1 = $this->formBuilder->search('foo'); @@ -310,6 +333,29 @@ public function testFormTime() $this->assertEquals('<input class="span2" name="foo" type="time">', $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('<input name="foo" type="url">', $form1); + $this->assertEquals('<input name="foo" type="url" value="http://foobar.com">', $form2); + $this->assertEquals('<input class="span2" name="foo" type="url">', $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('<input name="foo" type="week">', $form1); + $this->assertEquals('<input name="foo" type="week" value="' . \Carbon\Carbon::now()->format('Y-\WW') . '">', + $form2); + $this->assertEquals('<input class="span2" name="foo" type="week">', $form3); + } + public function testFormFile() { $form1 = $this->formBuilder->file('foo'); From 6d1ccebb53ca4ae3d56ead949a39a7eb851ca16b Mon Sep 17 00:00:00 2001 From: Victor Isadov <brahman63@mail.ru> 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 ef918176..a1f1da60 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 @@ public function getValueAttribute($name, $value = null) 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 @@ public function getValueAttribute($name, $value = null) } } + /** + * 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 @@ public function getValueAttribute($name, $value = null) */ protected function request($name) { - if (! isset($this->request)) { + if (!$this->considerRequest) { + return null; + } + + if (!isset($this->request)) { return null; } @@ -1312,16 +1332,16 @@ public function old($name) $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 e53f0072..6936a5f2 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -31,10 +31,10 @@ public function setUp() $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 @@ public function tearDown() 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('<input name="person[name]" type="text" value="John">', $name); $this->assertEquals('<input name="person[surname]" type="text" value="Doe">', $surname); - $checked = $this->formBuilder->checkbox("aggree", 1); + $checked = $this->formBuilder->checkbox("agree", 1); $unchecked = $this->formBuilder->checkbox("no_value", 1); - $this->assertEquals('<input checked="checked" name="aggree" type="checkbox" value="1">', $checked); + $this->assertEquals('<input checked="checked" name="agree" type="checkbox" value="1">', $checked); $this->assertEquals('<input name="no_value" type="checkbox" value="1">', $unchecked); $checked_array = $this->formBuilder->checkbox("checkbox_array[]", 1); @@ -67,10 +68,17 @@ public function testRequestValue() $this->assertEquals('<input checked="checked" name="checkbox_array[]" type="checkbox" value="1">', $checked_array); $this->assertEquals('<input name="checkbox_array[]" type="checkbox" value="4">', $unchecked_array); - $checked = $this->formBuilder->radio("aggree", 1); + $checked = $this->formBuilder->radio("agree", 1); $unchecked = $this->formBuilder->radio("no_value", 1); - $this->assertEquals('<input checked="checked" name="aggree" type="radio" value="1">', $checked); + $this->assertEquals('<input checked="checked" name="agree" type="radio" value="1">', $checked); $this->assertEquals('<input name="no_value" type="radio" value="1">', $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('<input name="person[name]" type="text" value="Not John">', $name); + $this->assertEquals('<input name="person[surname]" type="text" value="Not Doe">', $surname); } public function testOpeningForm() From fda9d3dad85ecea609ef9c6323d6923536cf5643 Mon Sep 17 00:00:00 2001 From: ThunderbirdsX3 <THUNDER_BIRDS_No.XIII@live.com> 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 17da19f5..743b32a0 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -455,6 +455,10 @@ protected function attributeElement($key, $value) 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 6936a5f2..35d43c0e 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -792,6 +792,18 @@ public function testBooleanAttributes() $this->assertEquals('<textarea readonly name="test" cols="50" rows="10"></textarea>', $input); } + public function testArrayClassAttributes() + { + $input = $this->formBuilder->text('test', null, ['class' => ['class-a', 'class-b']]); + $this->assertEquals('<input class="class-a class-b" name="test" type="text">', $input); + + $input = $this->formBuilder->text('test', null, ['class' => [ + 'class-a', + false ? 'class-b' : 'class-c' + ]]); + $this->assertEquals('<input class="class-a class-c" name="test" type="text">', $input); + } + protected function setModel(array $data, $object = true) { if ($object) { diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index a5a9ee27..f79320af 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -151,4 +151,18 @@ public function testBooleanAttributes() $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 <tautvydas.rasimavicius@gmail.com> 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 a1f1da60..c15ef66b 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -737,7 +737,7 @@ public function selectMonth($name, $selected = null, $options = [], $format = '% */ 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 @@ protected function optionGroup($list, $label, $selected, array $attributes = [], $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 35d43c0e..b343cc14 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -537,6 +537,58 @@ public function testSelectCollection() ); $this->assertEquals($select, '<select name="size"><option value="L" data-foo="bar" disabled>Large</option><option value="S">Small</option></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 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( + '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, + '<select name="size"><optgroup label="Large sizes"><option value="L" disabled>Large</option><option value="XL">Extra Large</option></optgroup><option value="M" disabled>Medium</option><optgroup label="Small sizes" disabled><option value="S">Small</option><option value="XS">Extra Small</option></optgroup></select>' + ); } public function testFormSelectRepopulation()