Laravel – 5.1 – Middleware For Input Validation

Actually this article is about form validation using a single middleware. Well, Laravel peovided us many ways for validating our user inputs and my favorite way is using Form Request class which is just awesome. Chexk it here if you are not already aware of that. Using a form request class we can validate the incoming request before the coresponding controller method’s code gets executed and to implement this we need to make/create form request class for each controller as per method basis and we declare only one method in that form request class which is the rules() to declare validation rules for validating the incoming request.

Well, nothing bad in it but I just want to keep my validation rules inside my model and also want to use only a single class for validating all of my models. So, to achieve this, I’ve replaced the form request class using a single middleware and here is how I did it.

At first let’s create the middleware:

Now, we need to add this middleware in the $routeMiddleware array in the App\Http\Kernel.php class, for example, the $routeMiddleware is given below:

Now, to use this middleware as form validator for our every model we may use this middleware in the route declaration as given below:

Here, the Validator is the middleware (alias of original middleware (App\Http\Middleware\InputValidator) class) and the parameter passed into the middleware is App\Eloquent\Tag to tell the middleware which model to use to get the validation rules so let’s create the model with rules() method, for example, my models are saved in app/Eloquent directory and namespace is App\Eloquent so fully qualified name of the model class was passed as the middleware parameter so we can obtain an instance of that model easily.Let’s check out the model now:

That’s it. Now, I can validate the request inputs (Submitted Form Inputs) using the middleware and this way I can keep my validation rules inside the model it self and most importantly I can now use only this single middleware to validate all of my models instead of using an individual FormRequest class for each model. This way still my validation service is out of my controller which runs before the controller method gets called and everything goes very well. That’s it.

  • Josué Camelo dos Santos

    Very Good!

    • Thank you @josucamelodossantos:disqus 🙂

  • karol

    How about multiple context validation groups? 🙂

    • Such as ?

    • Sidney

      I have the same question @disqus_fdZWH5VQA0:disqus. Yii 2 has ‘scenarios’. AFAIK, It seems laravel lacks this feature. Maybe you have to do some conditional validations in some places of your app, something I dont think good.

  • Sidney

    By the way, great solution for the ‘rules in the model’, @sheikh_heera:disqus. I wonder why Laravel has not this natively. Should not the own models dictate its rules? Is there any reasons for Laravel does not do?

    • Thanks. It’s not limited to use this rule because there are other ways and people have different opinions on validating a model, for example, some developers use request class to validate the form and put everything in there. Just a preference depending on the context.

  • Robert Roy

    great answer … this answer solve my problem thanks alot 🙂

    • You are welcome @@disqus_SOBwwE7sfZ:disqus 🙂

Latest Blog