Things to know in Laravel : chapter four

validation rule

Introduction

Today we’re going to talk about Laravel Validation . Validation is one of the most important aspects for an application . Without these, will lead to security vulnerability and might even break our websites. Validation is quite simple yet powerful in Laravel . App\Http\Controllers\Controller uses ValidatesRequests trait to include validation functions on our controller.

List of contents

  1. Simple validation
  2. Form Request Validation
  3. Drying up the Form Request
  4. Creating a custom validation rule

Getting Started

Laravel validation couldn’t be more easier than we can ever imagine . Laravel provides an elegant and easy to remember validation rules . There are several approaches for handling validation in laravel like $request->validate() , FormRequest .

Let’s look at this simple validation .

Looks readable and neat right ? 🙂 We are validating our requests with rules like required, unique and max length .

In above example , we are specifying that

title is required , should be unique and maximum length is 255

body is required

publish_at field may be either null or a valid date representation .

This is one way for doing form validation . Well this seems pretty good but sooner or later our controller will be bloated with validation logic . Now how do we validate and write elegant code . This is where Form Request swoop in.

Form Request Validation

Form requests are custom request classes containing validation logic . Let’s create a form request class using artisan command .

We have created two request classes for storing and updating . Let’s take a look at the StoreRequest while adding previous validation we did on our controller .

Now all we have to do is update on our controller . Let’s inject the StoreRequest in our store function.

Now the validated requests will only be available and we can save them directly. We can do the same things for update requests too .

Drying Up the From Request

All seems pretty good up to now . But remember we are creating two classes for store and update , these both classes extends Illuminate\Foundation\Http\FormRequest . How can we dry up this two classes ?

Things to remember while drying up these two classes :

  • We are using same rules on both classes like unique, max, required .
  • The rules unique will differ for update class ( it want id to be appended for update case )
  • The validation messages may differ .

Wow , after listing out above things to remember this seems hard but worry not , this is easy 🙂  . Let’s dry out our from validation .

StoreRequest and UpdateRequest both extends FormRequest , Let’s first update our UpdateRequest . Now, UpdateRequest will extend StoreRequest instead . Let’s take a look at below code for the final look of our Request.

We are using array_merge() PHP function . This function simply merge two arrays .

We have come to the point where we want to create our own custom validation rule after all today we learn to validate form requests , create a separate form validation classes and also dry it out ,now why don’t we create our own validation rule right ? 🙂

At ease , we will create our own simple custom validation rule .

Creating a custom validation rule

We can use artisan command , php artisan make:rule CustomValidation . The Rule class will be created in App/Rules once you enter the command.

The above validation can be use for email validation  which looks for valid domain and has an MX record. Now using above rule :

This is just an easy demonstration but you can play around with complex logic in your custom validation rule . We have covered a lot of topic in this article . Hope this help you understand using the validation the right way .

If you have any queries or suggestions for us, feel free to contact us here.

Happy coding 🙂

Sharing is caring!

Related Post