Skip to content

Commit

Permalink
Merge pull request #649 from rogermui/route-param
Browse files Browse the repository at this point in the history
redo pull request #627 with backwards compatibility
  • Loading branch information
mlantz authored May 19, 2020
2 parents 8951151 + d2910a7 commit 5ef9a3c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/FormBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
19 changes: 19 additions & 0 deletions tests/FormBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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('<form method="GET" action="http://localhost/user/1?foo=bar" accept-charset="UTF-8">', $form1);
$this->assertEquals('<form method="GET" action="http://localhost/user/2?foo=bar" accept-charset="UTF-8">', $form2);
$this->assertEquals('<form method="GET" action="http://localhost/user/3?foo=bar" accept-charset="UTF-8">', $form3);
$this->assertEquals('<form method="GET" action="http://localhost/user/4?foo=bar" accept-charset="UTF-8">', $form4);
}

public function testClosingForm()
{
$this->assertEquals('</form>', $this->formBuilder->close());
Expand Down

0 comments on commit 5ef9a3c

Please sign in to comment.