Laravel 5 validation example | How to write validation rules in Laravel | Laravel form validation

While developing the web application we need server side form validations. Laravel does this task beautifully. In this article I'm going to explain how we can do our form validations in Laravel.

We don't need to write the form validations in our Controller code like generally people does.

Here is an example of writing form validation rules in Laravel.

For example here is the form:

When you will submit the form it will go to register route like:

It says the register route has routed to 'postRegister' function which is inside the 'RegisterController'.


Now if you see here in 'postRegister' we have passed RegisterRequest for the form validation. So we are going to write validation rules inside RegisterRequest.

Only after validation RegisterRequest rules the code of 'postRegister' can execute else if validation fails it return back with validations errors.

I have created a Requests directory inside 'Http'.


Here in this class I have written all the validation rules. We also need to call atuthorize() function and return true for authorizing the Request.

We also can customize our validation errors by overriding the message function.

Like in above code I have done it for required name.

If I don't customize the error it will return like 'Name is required' but because I have customized it so it will return the customized error.

Here are some of the validation rules example in Laravel:

Now if you wanted to print all the returning validation errors to show the user, The below code will help you to print all the validation errors.


Share This:

Leave a Reply

Your email address will not be published. Required fields are marked *