Laravel

Laravel 5 Custom Validation

Laravel 5 Custom Validation

Hi In this tutorial I am going to guide you to create custom validation rule and messages. Sometimes default validation rules are not enough to validate inputs So we are forcing to create a custom validation by own. Here Let us see how to make proper custom validation in step by step.

Steps :

1. Create Service Provider
2. Write your required validation code.
3. Map Service Provider.
4. Call your rule in your Form Request file.
5. Add Custom Error Message

1. Create Service Provider

The reason of using Service Provider is, Service Provider is the global place for laravel application.

By using command create Service Provider

Here I have named my Service provider as ValidatorServiceProvider, It will create ValidatorServiceProvider.php file under App/Providers folder.

ValidatorServiceProvider.php 

2. Write your required validation code.

Here I have written my custom code to validate The URL is valid or not in inside boot() function



3. Map Service Provider.

Now Call your ServiceProvider in app.php providers array.

So you can access your ValidatorServiceProvider to entire application.

4. Call your rule in your Form Request file.

As like other default rules you can access you custom rule also.

For Example :

 5. Add Custom Error Message

You can find validation error messages at resources/lang/en/validation.php

Here add you custom error messages.
For Example :

Thats All now your custom validation rule will work like charm.
Thanks for reading this article, If you like this article don’t forget to share and comment.
Happy Coding!.