Laravel 5 create Custom Validation Rule example.

By Hardik Savani | March 3, 2016 | | 74849 Viewer | Category : PHP Laravel


Custom Validation Rule is very usefull and interesting concept of laravel 5 because custom validation throught we can re-use validation and use it easily by just name as like laravel core validation(required,same,in etc.). So, In this post i am going to show you how to create custom validation rules in laravel 5.2. you can create very simple way and use too. you can create custom validation rules by few following step and you can see preview of example that give for add custom validation rule. In this example you are creating is_odd_string custom validation rule.

Preview

Step 1: Create Route and Controller

In first step we will create two route for example, one for get method will help to generate view and second for post method that help for from submit. open your routes.php file and put bellow code:

app/Http/routes.php

Route::get('customVali', 'CustomValDemoController@customVali');

Route::post('customValiPost', 'CustomValDemoController@customValiPost');

Ok, now we have to create controller for handle route method so create CustomValDemoController controller in your project and put following code:

app/Http/Controllers/CustomValDemoController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CustomValDemoController extends Controller

{

public function customVali()

{

return view('customVali');

}

public function customValiPost(Request $request)

{

$this->validate($request, [

'title' => 'required|is_odd_string',

]);

print_r('done');

}

}

Step 2: Declare Validation

In this step we will declare and write code of our custom validation. So first open app/Providers/AppServiceProvider.php file and put following code:

app/Providers/AppServiceProvider.php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use Validator;

class AppServiceProvider extends ServiceProvider

{

public function boot()

{

Validator::extend('is_odd_string', function($attribute, $value, $parameters, $validator) {

if(!empty($value) && (strlen($value) % 2) == 0){

return true;

}

return false;

});

}

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.php and put one line as i did bellow:

resoueces/lang/en/validation.php

return [

'is_odd_string' => "The :attribute must be even string lenght.",

'accepted' => 'The :attribute must be accepted.',

.....

]

Step 3: Create View

this is a last step and you have to create one view for demo. so just copy bellow code and put on following file.

resoueces/view/customVali.blade.php

<html lang="en">

<head>

<title>Custom Validation Rule Laravel 5</title>

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" >

</head>

<body>

<nav class="navbar navbar-default">

<div class="container-fluid">

<div class="navbar-header">

<a class="navbar-brand" href="#">Custom Validation Rule Laravel 5</a>

</div>

</div>

</nav>

<div class="container">

<form action="{{ URL::to('customValiPost') }}" class="form-horizontal" method="post">

@if (count($errors) > 0)

<div class="alert alert-danger">

<ul>

@foreach ($errors->all() as $error)

<li>{{ $error }}</li>

@endforeach

</ul>

</div>

@endif

<input type="text" name="title" class="form-control" style="width:30%" placeholder="Add Odd String" />

<br/>

<button class="btn btn-primary">Save</button>

</form>

</div>

</body>

</html>

Finally, we craeted custom validation rule now you can check....