From d2910a7a978bdb2df485e0d27b0c549c55ea35d7 Mon Sep 17 00:00:00 2001 From: Roger Mui Date: Mon, 18 May 2020 16:26:17 +1000 Subject: [PATCH] redo pull request #627 with backwards compatibility --- src/FormBuilder.php | 8 +++++++- tests/FormBuilderTest.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/FormBuilder.php b/src/FormBuilder.php index 4cd0937d..6a228ff7 100644 --- a/src/FormBuilder.php +++ b/src/FormBuilder.php @@ -1201,7 +1201,13 @@ protected function getUrlAction($options) protected function getRouteAction($options) { if (is_array($options)) { - return $this->url->route($options[0], head(array_slice($options, 1))); + $parameters = array_slice($options, 1); + + if (array_keys($options) === [0, 1]) { + $parameters = head($parameters); + } + + return $this->url->route($options[0], $parameters); } return $this->url->route($options); diff --git a/tests/FormBuilderTest.php b/tests/FormBuilderTest.php index 06a8b450..2dc3379e 100644 --- a/tests/FormBuilderTest.php +++ b/tests/FormBuilderTest.php @@ -4,6 +4,7 @@ use Collective\Html\HtmlBuilder; use Illuminate\Contracts\View\Factory; use Illuminate\Http\Request; +use Illuminate\Routing\Route; use Illuminate\Routing\RouteCollection; use Illuminate\Routing\UrlGenerator; use Illuminate\Support\Collection; @@ -99,6 +100,24 @@ public function testOpeningForm() $form5); } + public function testFormRoute() + { + $routes = new RouteCollection(); + $routes->get('user/{id}'); + $routes->add(new Route(['GET'], 'user/{id}', ['as' => 'user.show'])); + $this->urlGenerator->setRoutes($routes); + + $form1 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', 1, 'foo' => 'bar']]); + $form2 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', 'id' => 2, 'foo' => 'bar']]); + $form3 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', [3, 'foo' => 'bar']]]); + $form4 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', ['id' => 4, 'foo' => 'bar']]]); + + $this->assertEquals('
', $form1); + $this->assertEquals('', $form2); + $this->assertEquals('', $form3); + $this->assertEquals('', $form4); + } + public function testClosingForm() { $this->assertEquals('
', $this->formBuilder->close());