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
app/Http/routes.phproutes.php
file.
- Route::get('custom-validation',['as'=>'custom-validation.get','uses'=>'CustomValidatorController@getCustomValidation']);
- Route::post('custom-validation',['as'=>'custom-validation.post','uses'=>'CustomValidatorController@postCustomValidation']);
In this step, we will create a
app/Http/Controllers/CustomValidatorController.phpCustomValidatorController.php
file and put following code.
- <?php
- namespace App\Http\Controllers;
- use Illuminate\Http\Request;
- use App\Http\Controllers\Controller;
- class CustomValidatorController extends Controller {
- public function getCustomValidation(){
- return view('customvalidation');
- }
- public function postCustomValidation(Request $request){
- $this->validate($request, [
- 'phone' => 'required|in_phone',
- ]);
- return 'successfully';
- }
- }
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 App\Providers;
- use Illuminate\Support\ServiceProvider;
- 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',
- ],
- ],
In this last step, we will create a view file where we can check this validation rules.
So create a
resoueces/view/customvalidation.blade.phpcustomvalidation.blade.php
file in following path resoueces/view/
- <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 class="container">
- {!! Form::open(array('route' => 'custom-validation.post','method'=>'POST')) !!}
- @if (count($errors) > 0)
- <div class="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>
Click here to see demo..
0 comments