Laravel 5 create Custom Validation Rule example.

Laravel 5 create Custom Validation Rule example.

In this post we will give you information about Laravel 5 create Custom Validation Rule example.. Hear we will give you detail about Laravel 5 create Custom Validation Rule example.And how to use it also give you demo for it if it is necessary.

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.


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:


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:


namespace AppHttpControllers;

use IlluminateHttpRequest;

class CustomValDemoController extends Controller


public function customVali()


return view('customVali');


public function customValiPost(Request $request)


$this->validate($request, [

'title' => 'required|is_odd_string',





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:


namespace AppProviders;

use IlluminateSupportServiceProvider;

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:


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.


<html lang="en">


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

<link rel="stylesheet" href="#" >



<nav >

<div >

<div >

<a href="">Custom Validation Rule Laravel 5</a>




<div >

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

@if (count($errors) > 0)

<div >


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

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





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


<button >Save</button>





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

Hope this code and post will helped you for implement Laravel 5 create Custom Validation Rule example.. 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

For More Info See :: laravel And github

Leave a Comment

Your email address will not be published. Required fields are marked *

  +  41  =  42

We're accepting well-written guest posts and this is a great opportunity to collaborate : Contact US