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
1 2 3 4 | Route::get('custom-validation','DemoController@customValidation'); Route::post('savedata','DemoController@savedata'); |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class DemoController extends Controller { function customValidation(){ return view('form_view'); } function savedata(Request $request){ $this->validate($request, [ 'phone' => 'required|indian_phone', ]); return 'successfully'; } } ?> |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | <?php namespace App\Providers; use Illuminate\Support\ServiceProvider; use Illuminate\Support\Facades\Schema; use Validator; class AppServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { Schema::defaultStringLength(191); Validator::extend('indian_phone', function($attribute, $value, $parameters) { return substr($value, 0, 3) == '+91'; }); } /** * Register any application services. * * @return void */ public function register() { // } } |
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
1 2 3 4 5 6 7 | 'custom' => [ 'phone' => [ 'indian_phone' => 'Please enter Indian phone number which starts with +91', ], ], |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | <!DOCTYPE html> <html lang="en"> <head> <title>Custom Validation In Laravel</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container"> <div class="panel panel-primary"> <div class="panel-heading">Custom Validation In Laravel </div> <div class="panel-body"> <div class="panel-body"> <div class="row"> @if (count($errors) > 0) <div class="alert alert-danger"> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif <form action="{{url('/')}}/savedata" method="post" id="sitelogo"> {{csrf_field() }} <div class="form-group"> <label for="phone">Phone: </label> <input type="text" class="form-control" id="phone" name="phone"> </div> <button type="submit" class="btn btn-default">Submit</button> </form> </div> </div> </div> </div> </div> </body> </html> |
I hope you might have understood it properly, keep reading our other blog posts for more coding tricks.
Thanks!!
I’m a full-stack developer. My hobby and profession to write blog and programming tips that helps to others. I am a great admirer of PHP, Laravel, Codeigniter, AngularJS, Vue.js, Javascript, JQuery, WordPress, Plugin Development, Theme Development and Bootstrap from the early stage.