This tutorial is out of date and no longer maintained.
Today we’ll be handling form validation in Laravel. There are many things that need to happen to make sure that everything works the way a user would expect it to so let’s get started.
Our application will do many things. These include:
That’s a lot to do so let’s get started. We’ll start by setting up our database.
Once we’ve got Laravel all set up, let’s go to our command line and create our migration so that our database table will be created. We will need this so we can do validation to make sure that emails entered are unique in the database. We wouldn’t want multiple users with the same email right?
Go into the command line and type
Now let’s edit that newly generated migration file.
Now make sure your database settings are good in app/config/local/database.php
or app/config/database.php
and then let’s run our migration:
We are going to create our Eloquent model so that we can save our ducks to the database. This is also how we will be able to test to make sure that our new duck/user has a unique email in the database. Create the model at app/models/Duck.php
.
With our database and model ready to go, now we can get to our actual validation.
Further Reading: A Guide to Using Eloquent ORM in Laravel
We will be handling the routing for our application in the app/routes.php
file that Laravel provides. We’ll be handling the GET
for showing the form and the POST
for processing the form here.
This will be accessible at http://example.com/ducks
and then we’ll also be POSTing to the same URL. With our routes ready, let’s create the duck-form that we are displaying to our user.
Further Reading: Simple and Easy Laravel Routing
The view file will be at app/views/duck-form.blade.php
and we’ll use Laravel’s Blade to handle our views.
Further Reading: Simple Laravel Layouts Using Blade
Here’s that view file. We’ll use Bootstrap to make our views look good and then we’ll start our validation.
Now our beautiful form has taken… well… form.
Now that we have our view, we’ll be going through all the validations needed. Since we already have our form’s action="/ducks"
ready to go, our form will send a POST
request to http://example.com/ducks
and then we handle that in the route we made earlier in app/routes.php
.
Let’s start off our validation now in that routes.php
file. We’re going to create our rules, run the validation on the form inputs, and handle the error messages from there.
Let’s create those rules:
With this setup, we have:
name
email
that has to be in email form
password
password_confirm
field that needs to match passwordSo we have set the rules for our inputs. We have run the validation and checked if that worked. If it didn’t work, we are sending our user back to our form with the errors. If the validation succeeded, we are going to create the duck in our database and redirect the user back to the form. For a full list of the validation rules available to you, go look at the available validation rules.
Let’s say that our user did not pass the validation. We want to show off all the messages in our view. All we have to do is go into our view and add that in.
Just like that, we know can show off our errors. This may not always be ideal though. Sometimes our user will expect the error to be placed next to the input it corresponds to.
We have the ability to do this in Laravel and we can pick out single errors using:
We can add this to each of our inputs and also we will use an if statement to add a Bootstrap error class (has-error
) so that Bootstrap will turn our inputs red.
Now we are using if statements that will show errors if Laravel has shown there is an error for that field. Each input will have the correct errors next to it now.
Now that our errors are good to go, our users will find those messages helpful. One other thing that they would appreciate also is that they would probably want to have the inputs they provided to still populate the form. We wouldn’t want our users to enter their name and email all over again just because their two password fields didn’t match up. We have to add something to our form validation on the backend side of things and then we’ll add the information to our view file.
We are redirecting users back to the form with all inputs except the password
and password_confirm
fields. This way they won’t have to enter their name and email again. Now, we have to populate our form with the data that comes back. We’ll use our input
field’s value
attribute and the way we get old input data from Laravel is to use:
We’ll add that to our form now.
Just like that, we are now populating our form with data that the user submitted. Our users will thank us for not having to enter information again, especially on larger forms!
<– https://cask.scotch.io/2014/07/laravel-form-validation-old-input.png –>
The last thing we’ll handle in form validation for Laravel today is to create custom error messages. Sometimes the default ones just aren’t fun enough for our flare and pizazz. All we have to do for this is to create our custom messages and pass them into the $validator
in our routes.php
file. Let’s change the messages for our required
and same
fields.
By just adding that Laravel will automatically send back those error messages to the view. We don’t have to change anything on the view side of our code.
You can create your own messages by using the :attribute
and :other
fillers to fill that space with the field name.
Another way to set rules for attributes is to do it in your models. This is a great way to do this since all the rules are attached to the model. This way, no matter where you have to validate, you can always reference back to the model. Let’s look at how we can do this. In your model, let’s create the same rules we created earlier:
That’s all we have to do in the model. Now to access those rules when we validate, we just pass in those rules using ModelName::$rules
.
You can also use this method to set rules based on user levels, like for an admin vs a normal user with differently named rule sets. So you would be able to create $adminRules
and $userRules
and access them directly. Thanks to longshot for pointing out this great Jeffrey Way tip.
Hopefully, this was a good example of the flexibility and features that Laravel provides when validating forms. There is still much more to dive into like creating custom validation rules and conditionally adding rules. I would definitely suggest checking out the official docs for more information about the great things you can do.
Let us know if you have any questions or comments on any validation tactics and thanks for reading.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
This textbox defaults to using Markdown to format your answer.
You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!