Please note that this chapter was written for VERSION 3 of the Laravel PHP Framework.
Forms are an important part of any web-based application. They help control the flow of the application which allows us to receive input from our users and make decisions that affect the functionality of our applications. They are also my least favourite thing in the world to write.
Fortunately for me (and you), Laravel's form class takes care of a lot of the hard work for us by providing useful methods for generating common form elements. Let's use the form class to create a simple web form in one of our views.
Creating Forms
// form.php
<?php echo Form::open('my/route'); ?>
<!-- username field -->
<?php echo Form::label('username', 'Username'); ?>
<?php echo Form::text('username'); ?>
<!-- password field -->
<?php echo Form::label('password', 'Password'); ?>
<?php echo Form::password('password'); ?>
<!-- login button -->
<?php echo Form::submit('Login');
<?php echo Form::close(); ?>
Take a moment, stare at the form source, you have never seen a form so clean. Say it out loud to yourself, go on.. I will wait.
I have never seen a form so clean.
You are right, it's beautiful. Let's have a look at the generated source to make sure I'm not just teaching you wrong, you know, for fun?
<form method="POST" action="http://mysite/my/route" accept-charset="UTF-8">
<!-- username field -->
<label for="username">Username</label>
<input type="text" name="username" id="username">
<!-- password field -->
<label for="password">Password</label>
<input type="password" name="password" id="password">
<!-- login button -->
<input type="submit" value="Login">
</form>
Great, it worked! I mean of course it did! Let's go over the form line by line to see how it works. On our first line we have the Form::open()
method which creates a form open tag for us.
<?php echo Form::open('my/route'); ?>
The first parameter to the method is the URI we wish to submit the form to. The second parameter is the METHOD
used to submit the form, if you don't provide a method as a string Laravel will assume that you want a POST
form, which is the most common usage.
The third parameter is also optional. You can pass an array of attribute => value
pairs to add extra attributes to the <form>
tag. For example, if you wished to target the form with some Javascript you may want to pass array('id' => 'myform')
as the third parameter to give the element an id
.
To submit a form to a secure URI (https) you will need to use the open_secure()
method instead of open()
. It accepts the same parameters.
If you wish to be able to have files uploaded from your form then it will need to use multipart/data
We can use open_for_files()
instead of the open()
method to accomplish this. This method also accepts the same parameters.
Finally if you wish to submit to a secure URI and have files uploaded you will need to use the open_secure_for_files()
method. This once again accepts the same parameters and is a combination of both open_secure()
and open_for_files()
.
Adding Labels
The next line contains the Form::label()
method which is used to create a <label>
element. The first parameter is the name
of the input element that it describes to be used in the for=""
attribute. The second parameter is what will be used as the content of the label
element. You can pass an array as an optional third parameter to apply extra HTML element attributes.
Generating Inputs
Next we have the input generators, these methods help to generate all of the HTML elements that are common to forms. In the example above we use the text()
and password()
method to generate <input type="text"..
and <input type="password"..
elements.
The first parameter to the method, is the value of the elements name
attribute. The optional second parameter is the default value of the element. Once more we can pass an array of HTML attributes as an optional third parameter. Are you starting to see a pattern yet?
The textarea()
and hidden()
methods also accept the same parameters as the text()
method.
Check-boxes can be created using the checkbox()
method, with the first parameter being the name of the element and the second being the value. The third option is an optional boolean switch to set whether the element is initially checked or not. It will default to false. The fourth optional parameter again sets attributes. In fact, go ahead and assume that all future inputs accept an attributes array as their optional final parameter. Let's have a look at a check-box generator..
<?php echo Form::checkbox('admin', 'yes', true, array('id' => 'admin-checker')); ?>
The radio()
method creates radio buttons and shares the same parameters as the checkbox()
method.
Next we have drop downs, the most awkward of all form elements. Fortunately, all we need to do is pass a name, an array of value => label
options, and an optional parameter to state which option should be selected by default to the select()
method. Our drop-down will then be generated for us, for example..
<?php
Form::select('roles', array(
0 => 'User',
1 => 'Member',
2 => 'Editor',
3 => 'Administrator'
), 2);
and we get..
<select name="roles">
<option value="0">User</option>
<option value="1">Member</option>
<option value="2" selected="selected">Editor</option>
<option value="3">Administrator</option>
</select>
Great! Now it's time to submit our form.
Generating Buttons
The submit()
and button()
generator methods both accept the same parameters. The first being the value
of the HTML element, and the second being the usual array of attributes.
<?php
Form::submit('Login');
Form::button('Do other thing!');
Secret Inputs
There are also a number of extra generation methods for less common form inputs that aren't covered by the documentation. Many of these inputs were added with the HTML5 spec, and are in the process of being implemented by good web browsers (not IE). Here is a listing of the methods along with their parameters.
<?php
// create a search field
Form::search($name, $value = null, $attributes = array());
// a textbox that only allows email addresses
Form::email($name, $value = null, $attributes = array());
// this text field will only allow phone numbers
Form::telephone($name, $value = null, $attributes = array());
// valid URL's only
Form::url($name, $value = null, $attributes = array());
// a number field with rocker switches
Form::number($name, $value = null, $attributes = array());
// a date picker
Form::date($name, $value = null, $attributes = array());
// used for file uploads
Form::file($name, $attributes = array());
CSRF Token
If you intend to use the in built cross-site-request-forgery (CSRF) filter to protect your AJAX submitted forms, you can add the CSRF token to your form by using the method token()
. For example..
<?php
Form::token();
Form Macros
Laravel has provided many different input methods, but what if we need something a little more custom? Fortunately Laravel has provided the macro()
method to allow us to create our own input generators.
By passing an input name and a closure to the macro()
method we can define our own input generator, let's take a look.
<?php
Form::macro('shoe_size', function() {
return '<input type="shoe_size" />';
});
Now we can use the Form class to generate our shoe size field in the same way as any other input, for example..
<?php echo Form::shoe_size(); ?>
If you need to use parameters, simply add them as parameters to the closure. Enjoy creating your own unique input generators!
My books are available online for free to encourage learning. However, if you'd like for me to keep writing, then please consider buying a digital copy over at Leanpub.com.
It's available in PDF, ePub, and Kindle format, and contains a bunch of extras that you won't find on the site. I have a full-time job, and I write my books in my spare time. Please consider buying a copy so that I can continue to write new books from the comfort of my sofa!