php

Opening a New HTML Form

Problem

You want to start a form.

You know you could use the HTML <form> tag directly, but want to use Laravel’s Form facade.

Solution

Use the Form::open() method.

Usually, this is done in a Blade template. There are several ways to use this method.

Using defaults

{{ Form::open() }}

The HTML produced is.

<form method="POST" action="http://currenturl" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">

This starts a form, using the POST method, to the current URL and will add an accept-charset=”UTF-8″ to the form. Additionally, a hidden token is added.

To a specific url

Instead of passing an action you should pass a url value. This occurs in the only argument Form::open() accepts … an array.

{{ Form::open(array('url' => 'http://full.url/here')) }}

This produces the following HTML.

<form method="POST" action="http://full.url/here" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">

To a route

Instead of passing the action you should pass a route value to one of your named routes.

{{ Form::open(array('route' => 'named.route')) }}

If the route doesn’t exist an error will be produced. Otherwise the form’s action attribute becomes the full URL to the route.

<form method="POST" action="http://full.url/someplace" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">

To a controller action

This is where you use action.

{{ Form::open(array('action' => 'Controller@method')) }}

If the controller or method doesn’t exist an error will be produced. Otherwise the form’s action attribute becomes the full URL to the route that will call the specified controller and method.

<form method="POST" action="http://full.url/someplace" accept-charset="UTF-8">
<input name="_token" type="hidden" value="somelongrandom string">

Specifying different methods

You can use methods other than POST with your forms. Pass the ‘method’ you want in the array argument. Valid methods are ‘get’, ‘put’, ‘patch’, ‘post’, or ‘delete’.

{{ Form::open(array('method' => 'get')) }}

This will produce the following HTML.

<form method="GET" action="http://currenturl" accept-charset="UTF-8">

Notice there’s no token? The token is not added for GET methods.

See the discussion at the bottom of this recipe for how Laravel “fakes” the methods browsers can’t handle.

Specifying file uploads

If you pass a ‘files’ => true as one of the array arguments, the form will become suitable for file uploads.

{{ Form::open(array('files' => true)) }}

The form now has the enctype=”multipart/form-data” attribute.

<form method="POST" action="http://currenturl" accept-charset="UTF-8"
  enctype="multipart/form-data">
<input name="_token" type="hidden" value="somelongrandom string">

Discussion

How Laravel “fakes” methods browsers cannot handle.

The form methods PUT, PATCH, and DELETE cannot be handled by most browsers. So what Laravel does is make the method=”POST” and adds a hidden field.

{{ Form::open(array('method' => 'PUT')) }}

This produces the following.

<form method="POST" action="http://currenturl" accept-charset="UTF-8">
<input name="_method" type="hidden" value="PUT">
<input name="_token" type="hidden" value="somelongrandom string">

The framework is smart enough to translate those hidden fields and change the request type to match what’s desired.

About the author

laravelrecipies