How To Create Custom Validation Rules With Laravel 5
In this post we will give you information about How To Create Custom Validation Rules With Laravel 5. Hear we will give you detail about How To Create Custom Validation Rules With Laravel 5And how to use it also give you demo for it if it is necessary.
How To Create Custom Validation Rules With Laravel 5
Laravel provides so many helpful validation rules but if you want to define your own validation rules in Laravel 5 then you can easily define your own validation rules and then you can use your custom validation rules in your application whenever you need.
In this tutorial, I will tell you step by step to create custom validation rules to validate Indian phone number that start from +91.
Step 1: Add Routes
In first step, we will add two routes, First route will help to render view and second route will handle the post request.
Add this two routes in your routes.php
file.
app/Http/routes.php
- Route::get('custom-validation',['as'=>'custom-validation.get','uses'=>'CustomValidatorController@getCustomValidation']);
- Route::post('custom-validation',['as'=>'custom-validation.post','uses'=>'CustomValidatorController@postCustomValidation']);
Route::get('custom-validation',['as'=>'custom-validation.get','uses'=>'CustomValidatorController@getCustomValidation']); Route::post('custom-validation',['as'=>'custom-validation.post','uses'=>'CustomValidatorController@postCustomValidation']);
Step 2: Create Controller
In this step, we will create a CustomValidatorController.php
file and put following code.
app/Http/Controllers/CustomValidatorController.php
- <?php
- namespace AppHttpControllers;
- use IlluminateHttpRequest;
- use AppHttpControllersController;
- class CustomValidatorController extends Controller {
- public functiongetCustomValidation(){
- returnview('customvalidation');
- }
- public functionpostCustomValidation(Request $request){
- $this->validate($request,[
- 'phone'=>'required|in_phone',
- ]);
- return'successfully';
- }
- }
<?php namespace AppHttpControllers; use IlluminateHttpRequest; use AppHttpControllersController; class CustomValidatorController extends Controller { public function getCustomValidation(){ return view('customvalidation'); } public function postCustomValidation(Request $request){ $this->validate($request, [ 'phone' => 'required|in_phone', ]); return 'successfully'; } }
Step 3: Use the extend method on the Validator facade in AppServiceProvider
Basically the custom validator Closure takes four arguments :
- $attribute
- $value
- $parameters
- $validator
Ok now we will write code for new custom validation rules.
app/Providers/AppServiceProvider.php
- <?php
- namespace AppProviders;
- use IlluminateSupportServiceProvider;
- use Validator;
- class AppServiceProvider extends ServiceProvider
- {
- public functionboot()
- {
- Validator::extend('in_phone',function($attribute,$value,$parameters){
- returnsubstr($value,,3)=='+91';
- });
- }
- public functionregister()
- {
- }
- }
<?php namespace AppProviders; use IlluminateSupportServiceProvider; use Validator; class AppServiceProvider extends ServiceProvider { public function boot() { Validator::extend('in_phone', function($attribute, $value, $parameters) { return substr($value, 0, 3) == '+91'; }); } public function register() { } }
In above code, in_phone is a rule name that will use later when i need to validate any phone number which must be start from +91.
Step 4: Define error message
Now, we will define custom error message for new validation rules.
resoueces/lang/en/validation.php
- 'custom'=>[
- 'phone'=>[
- 'in_phone'=>'Please enter Indian phone number which starts with +91',
- ],
- ],
'custom' => [ 'phone' => [ 'in_phone' => 'Please enter Indian phone number which starts with +91', ], ],
Step 5: Create View
In this last step, we will create a view file where we can check this validation rules.
So create a customvalidation.blade.php
file in following path resoueces/view/
resoueces/view/customvalidation.blade.php
- <htmllang="en">
- <head>
- <title>Laravel 5 : Custom Validation Rule</title>
- <linkrel="stylesheet"href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
- </head>
- <body>
- <divclass="container">
- {!! Form::open(array('route' => 'custom-validation.post','method'=>'POST')) !!}
- @if (count($errors) > 0)
- <divclass="alert alert-danger">
- <ul>
- @foreach ($errors->all() as $error)
- <li>{{ $error }}</li>
- @endforeach
- </ul>
- </div>
- @endif
- {!! Form::text('phone', old('phone'), ['placeholder' => 'Enter Indian phone number']) !!}
- <br/>
- {!! Form::submit('Save') !!}
- </form>
- </div>
- </body>
- </html>
<html lang="en"> <head> <title>Laravel 5 : Custom Validation Rule</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" > </head> <body> <div > {!! Form::open(array('route' => 'custom-validation.post','method'=>'POST')) !!} @if (count($errors) > 0) <div > <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif {!! Form::text('phone', old('phone'), ['placeholder' => 'Enter Indian phone number']) !!} <br/> {!! Form::submit('Save') !!} </form> </div> </body> </html>
Click here to see demo..
Hope this code and post will helped you for implement How To Create Custom Validation Rules With Laravel 5. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs