From e6bc7e5feacb303c72fa713989f8f5f842a76687 Mon Sep 17 00:00:00 2001 From: Tony Date: Thu, 14 Sep 2017 10:51:14 -0500 Subject: [PATCH 1/3] Adds getModel() It would be nice to be able to get the model before setting a new one (maybe in a partial) and having the original model to set back at the end of the partial. --- src/FormBuilder.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 2b69ac0..61fabf9 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -190,6 +190,16 @@ class FormBuilder { $this->model = $model; } + + /** + * Get the current model instance on the form builder. + * + * @return mixed $model + */ + public function getModel() + { + return $this->model; + } /** * Close the current form. From b39f51089792c786074c06f7678e5591d63ee632 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C4=B0zni=20Burak=20Demirta=C5=9F?= Date: Thu, 26 Oct 2017 10:37:42 +0300 Subject: [PATCH 2/3] + operator doesn't merge. Replaced by array_merge method. --- src/FormBuilder.php | 2 +- src/HtmlBuilder.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 2b69ac0..5ff6ea8 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -722,7 +722,7 @@ class FormBuilder { $selected = $this->getSelectedValue($value, $selected); - $options = ['value' => $value, 'selected' => $selected] + $attributes; + $options = array_merge(['value' => $value, 'selected' => $selected], $attributes); return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); } diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index d2ba3cd..aeeb160 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -94,7 +94,7 @@ class HtmlBuilder { $defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet']; - $attributes = $attributes + $defaults; + $attributes = array_merge($attributes, $defaults); $attributes['href'] = $this->url->asset($url, $secure); @@ -132,7 +132,7 @@ class HtmlBuilder { $defaults = ['rel' => 'shortcut icon', 'type' => 'image/x-icon']; - $attributes = $attributes + $defaults; + $attributes = array_merge($attributes, $defaults); $attributes['href'] = $this->url->asset($url, $secure); From 2c5db92f662bf5dfb7b198265168599941e29de6 Mon Sep 17 00:00:00 2001 From: Luiz Gustavo Martins Date: Mon, 5 Feb 2018 12:04:02 -0200 Subject: [PATCH 3/3] Fix some inputs treated like nestedModel when using an accessor (#471) * 5.6 Prep * Added $optgroupsAttributes parameter to select method. * escape HTML entities in `src`-attribute * Style fixes. Replaced == with ===. --- composer.json | 15 +++++---- src/Eloquent/FormAccessible.php | 2 +- src/FormBuilder.php | 60 ++++++++++++++++++++------------- src/HtmlBuilder.php | 6 ++-- tests/FormBuilderTest.php | 31 +++++++++++++++++ tests/HtmlBuilderTest.php | 3 ++ 6 files changed, 83 insertions(+), 34 deletions(-) diff --git a/composer.json b/composer.json index b7dda2f..30e2c2d 100755 --- a/composer.json +++ b/composer.json @@ -19,14 +19,14 @@ ], "require": { "php": ">=7.0.0", - "illuminate/http": "5.5.*", - "illuminate/routing": "5.5.*", - "illuminate/session": "5.5.*", - "illuminate/support": "5.5.*", - "illuminate/view": "5.5.*" + "illuminate/http": "5.6.x-dev", + "illuminate/routing": "5.6.x-dev", + "illuminate/session": "5.6.x-dev", + "illuminate/support": "5.6.x-dev", + "illuminate/view": "5.6.x-dev" }, "require-dev": { - "illuminate/database": "5.5.*", + "illuminate/database": "5.6.x-dev", "mockery/mockery": "~0.9.4", "phpunit/phpunit": "~5.4" }, @@ -39,6 +39,9 @@ ] }, "extra": { + "branch-alias": { + "dev-master": "5.6-dev" + }, "laravel": { "providers": [ "Collective\\Html\\HtmlServiceProvider" diff --git a/src/Eloquent/FormAccessible.php b/src/Eloquent/FormAccessible.php index de1c191..9b5d6d0 100644 --- a/src/Eloquent/FormAccessible.php +++ b/src/Eloquent/FormAccessible.php @@ -87,7 +87,7 @@ trait FormAccessible $mutator = collect($methods) ->first(function (ReflectionMethod $method) use ($key) { - return $method->getName() == 'form' . Str::studly($key) . 'Attribute'; + return $method->getName() === 'form' . Str::studly($key) . 'Attribute'; }); return (bool) $mutator; diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 32a793d..1c7e22d 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -575,6 +575,7 @@ class FormBuilder * @param string $selected * @param array $selectAttributes * @param array $optionsAttributes + * @param array $optgroupsAttributes * * @return \Illuminate\Support\HtmlString */ @@ -583,7 +584,8 @@ class FormBuilder $list = [], $selected = null, array $selectAttributes = [], - array $optionsAttributes = [] + array $optionsAttributes = [], + array $optgroupsAttributes = [] ) { $this->type = 'select'; @@ -609,8 +611,9 @@ class FormBuilder } foreach ($list as $value => $display) { - $optionAttributes = isset($optionsAttributes[$value]) ? $optionsAttributes[$value] : []; - $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes); + $optionAttributes = $optionsAttributes[$value] ?? []; + $optgroupAttributes = $optgroupsAttributes[$value] ?? []; + $html[] = $this->getSelectOption($display, $value, $selected, $optionAttributes, $optgroupAttributes); } // Once we have all of this HTML, we can join this into a single element after @@ -685,13 +688,14 @@ class FormBuilder * @param string $value * @param string $selected * @param array $attributes + * @param array $optgroupAttributes * * @return \Illuminate\Support\HtmlString */ - public function getSelectOption($display, $value, $selected, array $attributes = []) + public function getSelectOption($display, $value, $selected, array $attributes = [], array $optgroupAttributes = []) { if (is_array($display)) { - return $this->optionGroup($display, $value, $selected, $attributes); + return $this->optionGroup($display, $value, $selected, $optgroupAttributes, $attributes); } return $this->option($display, $value, $selected, $attributes); @@ -704,18 +708,21 @@ class FormBuilder * @param string $label * @param string $selected * @param array $attributes + * @param array $optionsAttributes * * @return \Illuminate\Support\HtmlString */ - protected function optionGroup($list, $label, $selected, array $attributes = []) + protected function optionGroup($list, $label, $selected, array $attributes = [], array $optionsAttributes = []) { $html = []; foreach ($list as $value => $display) { - $html[] = $this->option($display, $value, $selected, $attributes); - } + $optionAttributes = $optionsAttributes[$value] ?? []; - return $this->toHtmlString('' . implode('', $html) . ''); + $html[] = $this->option($display, $value, $selected, $optionAttributes); + } + + return $this->toHtmlString('html->attributes($attributes) . '>' . implode('', $html) . ''); } /** @@ -734,7 +741,12 @@ class FormBuilder $options = array_merge(['value' => $value, 'selected' => $selected], $attributes); - return $this->toHtmlString('html->attributes($options) . '>' . e($display) . ''); + $string = 'html->attributes($options) . '>'; + if ($display !== null) { + $string .= e($display) . ''; + } + + return $this->toHtmlString($string); } /** @@ -773,7 +785,7 @@ class FormBuilder return $selected->contains($value) ? 'selected' : null; } - return ((string) $value == (string) $selected) ? 'selected' : null; + return ((string) $value === (string) $selected) ? 'selected' : null; } /** @@ -854,7 +866,7 @@ class FormBuilder return $this->getRadioCheckedState($name, $value, $checked); default: - return $this->getValueAttribute($name) == $value; + return $this->getValueAttribute($name) === $value; } } @@ -907,7 +919,7 @@ class FormBuilder return $checked; } - return $this->getValueAttribute($name) == $value; + return $this->getValueAttribute($name) === $value; } /** @@ -1006,7 +1018,7 @@ class FormBuilder { $method = strtoupper($method); - return $method != 'GET' ? 'POST' : $method; + return $method !== 'GET' ? 'POST' : $method; } /** @@ -1108,7 +1120,7 @@ class FormBuilder // If the method is something other than GET we will go ahead and attach the // CSRF token to the form, as this can't hurt and is convenient to simply // always have available on every form the developers creates for them. - if ($method != 'GET') { + if ($method !== 'GET') { $appendage .= $this->token(); } @@ -1150,7 +1162,7 @@ class FormBuilder $old = $this->old($name); - if (! is_null($old) && $name != '_method') { + if (! is_null($old) && $name !== '_method') { return $old; } @@ -1161,7 +1173,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; @@ -1169,7 +1181,7 @@ class FormBuilder } $request = $this->request($name); - if (! is_null($request) && $name != '_method') { + if (! is_null($request)) { return $request; } @@ -1189,7 +1201,7 @@ class FormBuilder */ protected function request($name) { - if (!isset($this->request)) { + if (! isset($this->request)) { return null; } @@ -1227,16 +1239,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; } @@ -1253,7 +1265,7 @@ class FormBuilder */ public function oldInputIsEmpty() { - return (isset($this->session) && count($this->session->getOldInput()) == 0); + return (isset($this->session) && count($this->session->getOldInput()) === 0); } /** diff --git a/src/HtmlBuilder.php b/src/HtmlBuilder.php index 627cb03..7eb37ae 100755 --- a/src/HtmlBuilder.php +++ b/src/HtmlBuilder.php @@ -162,7 +162,7 @@ class HtmlBuilder $title = $this->entities($title); } - return $this->toHtmlString('attributes($attributes) . '>' . $title . ''); + return $this->toHtmlString('attributes($attributes) . '>' . $title . ''); } /** @@ -357,7 +357,7 @@ class HtmlBuilder { $html = ''; - if (count($list) == 0) { + if (count($list) === 0) { return $html; } @@ -451,7 +451,7 @@ class HtmlBuilder } // Treat boolean attributes as HTML properties - if (is_bool($value) && $key != 'value') { + if (is_bool($value) && $key !== 'value') { return $value ? $key : ''; } diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index d7b1a95..e6b1765 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -402,6 +402,37 @@ class FormBuilderTest extends PHPUnit_Framework_TestCase '' ); + $select = $this->formBuilder->select( + 'size', + [ + 'Large sizes' => [ + 'L' => 'Large', + 'XL' => 'Extra Large', + ], + 'M' => 'Medium', + 'Small sizes' => [ + 'S' => 'Small', + 'XS' => 'Extra Small', + ], + ], + null, + [], + [ + 'Large sizes' => [ + 'L' => ['disabled'] + ], + 'M' => ['disabled'], + ], + [ + 'Small sizes' => ['disabled'], + ] + ); + + $this->assertEquals( + $select, + '' + ); + $select = $this->formBuilder->select( 'encoded_html', ['no_break_space' => ' ', 'ampersand' => '&', 'lower_than' => '<'], diff --git a/tests/HtmlBuilderTest.php b/tests/HtmlBuilderTest.php index a5bcb8b..3cb6029 100644 --- a/tests/HtmlBuilderTest.php +++ b/tests/HtmlBuilderTest.php @@ -118,8 +118,11 @@ class HtmlBuilderTest extends PHPUnit_Framework_TestCase $result2 = $this->htmlBuilder->link("http://www.example.com", "Example.com", ["class" => "example-link"], null, false); + $result3 = $this->htmlBuilder->link("https://a.com/b?id=4¬_id=5", "URL which needs escaping"); + $this->assertEquals('<span>Example.com</span>', $result1); $this->assertEquals('Example.com', $result2); + $this->assertEquals('URL which needs escaping', $result3); } public function testMailto()