How To Create Custom Validation Rules With Laravel 5

How To Create Custom Validation Rules With Laravel 5

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
  1. Route::get('custom-validation',['as'=>'custom-validation.get','uses'=>'CustomValidatorController@getCustomValidation']);
  2. 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
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Http\Request;
  4. use App\Http\Controllers\Controller;
  5. class CustomValidatorController extends Controller {
  6. public function getCustomValidation(){
  7. return view('customvalidation');
  8. }
  9. public function postCustomValidation(Request $request){
  10.     $this->validate($request, [
  11.      'phone' => 'required|in_phone',
  12.      ]);
  13.      return 'successfully';
  14. }    
  15. }
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
  1. <?php
  2. namespace App\Providers;
  3. use Illuminate\Support\ServiceProvider;
  4. use Validator;
  5. class AppServiceProvider extends ServiceProvider
  6. {
  7. public function boot()
  8. {
  9. Validator::extend('in_phone', function($attribute, $value, $parameters) {
  10. return substr($value, 0, 3) == '+91';
  11. });
  12. }
  13. public function register()
  14. {
  15. }
  16. }

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
  1. 'custom' => [
  2. 'phone' => [
  3. 'in_phone' => 'Please enter Indian phone number which starts with +91',
  4. ],
  5. ],
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
  1. <html lang="en">
  2. <head>
  3.     <title>Laravel 5 : Custom Validation Rule</title>
  4.     <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >
  5. </head>
  6. <body>
  7.     <div class="container">
  8. {!! Form::open(array('route' => 'custom-validation.post','method'=>'POST')) !!}
  9.             @if (count($errors) > 0)
  10.              <div class="alert alert-danger">
  11.              <ul>
  12.              @foreach ($errors->all() as $error)
  13.              <li>{{ $error }}</li>
  14.              @endforeach
  15.              </ul>
  16.              </div>
  17.             @endif
  18.             {!! Form::text('phone', old('phone'), ['placeholder' => 'Enter Indian phone number']) !!}
  19.             <br/>
  20.             {!! Form::submit('Save') !!}
  21.         </form>
  22.     </div>
  23. </body>
  24. </html>

Click here to see demo..

Phone: (+91) 8800417876
Noida, 201301