How To Create Custom Validation Rules in Laravel

Laravel is the most powerful and very popular framework in PHP, Laravel provides so many helpful validations rules but if you want to define own validation rules then you can easily define rules. In this post, I am going to explain “How to create custom validation rule in Laravel 5”.

Custom validation rule is very useful and interesting concept of laravel, So I will show you how to create custom validation and use same as predefined laravel validation rules.

In this post, I will show you step by step create custom validation rule to validate Indian phone number that start with +91 .

Step 1 :- Create routes for display form and check validation before submit form

app/Http/routes.php

 

Step 2 :- In this create controller file like DemoController.php and copy below code and paste it in your file.

app/Http/Controllers/DemoController.php

 

Step 3 :- In this step we will declare and write code for custom validation rule, So first open app/Providers/AppServiceProvider.php

file and put the given code.

 

Now, we have to give error message for new created custom validation rules, so register custom message open resoueces/lang/en/validation.phpand put one line as i did bellow

resoueces/lang/en/validation.php

 

Step 4:- This is last step and you have to create one view for demo. so just copy bellow code and put on following file.

Laravel custom validation rule

 

View Demo
 

I hope you might have understood it properly, keep reading our other blog posts for more coding tricks.

Thanks!!

Add a Comment

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