Skip to content

Commit d2910a7

Browse files
author
Roger Mui
committed
redo pull request LaravelCollective#627 with backwards compatibility
1 parent 8951151 commit d2910a7

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/FormBuilder.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,13 @@ protected function getUrlAction($options)
12011201
protected function getRouteAction($options)
12021202
{
12031203
if (is_array($options)) {
1204-
return $this->url->route($options[0], head(array_slice($options, 1)));
1204+
$parameters = array_slice($options, 1);
1205+
1206+
if (array_keys($options) === [0, 1]) {
1207+
$parameters = head($parameters);
1208+
}
1209+
1210+
return $this->url->route($options[0], $parameters);
12051211
}
12061212

12071213
return $this->url->route($options);

tests/FormBuilderTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
use Collective\Html\HtmlBuilder;
55
use Illuminate\Contracts\View\Factory;
66
use Illuminate\Http\Request;
7+
use Illuminate\Routing\Route;
78
use Illuminate\Routing\RouteCollection;
89
use Illuminate\Routing\UrlGenerator;
910
use Illuminate\Support\Collection;
@@ -99,6 +100,24 @@ public function testOpeningForm()
99100
$form5);
100101
}
101102

103+
public function testFormRoute()
104+
{
105+
$routes = new RouteCollection();
106+
$routes->get('user/{id}');
107+
$routes->add(new Route(['GET'], 'user/{id}', ['as' => 'user.show']));
108+
$this->urlGenerator->setRoutes($routes);
109+
110+
$form1 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', 1, 'foo' => 'bar']]);
111+
$form2 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', 'id' => 2, 'foo' => 'bar']]);
112+
$form3 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', [3, 'foo' => 'bar']]]);
113+
$form4 = $this->formBuilder->open(['method' => 'GET', 'route' => ['user.show', ['id' => 4, 'foo' => 'bar']]]);
114+
115+
$this->assertEquals('<form method="GET" action="http://localhost/user/1?foo=bar" accept-charset="UTF-8">', $form1);
116+
$this->assertEquals('<form method="GET" action="http://localhost/user/2?foo=bar" accept-charset="UTF-8">', $form2);
117+
$this->assertEquals('<form method="GET" action="http://localhost/user/3?foo=bar" accept-charset="UTF-8">', $form3);
118+
$this->assertEquals('<form method="GET" action="http://localhost/user/4?foo=bar" accept-charset="UTF-8">', $form4);
119+
}
120+
102121
public function testClosingForm()
103122
{
104123
$this->assertEquals('</form>', $this->formBuilder->close());

0 commit comments

Comments
 (0)