Laravel create Custom Validation Rule example.

Today now in this post i will show you how to create custom validation rule in laravel. We know custom Validation Rule is very useful and very interesting concept of laravel because custom validation through we can re-use the validation and also use it easily by just name as like laravel core validation(required,same,in etc.). So, now in this post i am going to show you how to create a custom validation rules in laravel . we can create it very simple way and use too. We can create the custom validation rules by few following step .

is_odd_string custom validation rule.

Step 1: Create Route and Controller

Now in first step we need to create two route for example, one is for get method will help to generate the view and the second one for post method that will help you for from submit.So open your routes.php file and put the bellow code here:


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

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


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',
Step 2: Declare Validation

So now in this step we need to declare and need to write code of our custom validation. So at first open app/Providers/AppServiceProvider.php file and then put following code:


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 need to give error message for new created the custom validation rules, so need to register custom message open resoueces/lang/en/validation.php and then 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

So now this is the last step and we need to create one view for demo. so just need to copy bellow code and put on following file.


<html lang="en">
	<title>Custom Validation Rule Laravel </title>
	<link rel="stylesheet" href="" >
	<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 class="container">
		<form action="{{ URL::to('customValiPost') }}" class="form-horizontal" method="post">
			@if (count($errors) > 0)
			    <div class="alert alert-danger">
			            @foreach ($errors->all() as $error)
			                <li>{{ $error }}</li>
			<input type="text" name="title" class="form-control" style="width:30%" placeholder="Add Odd String" />
			<button class="btn btn-primary">Save</button>

Read Also : Laravel Join with Subquery in Query Builder Example

Thanks for read. I hope it help you. For more you can follow us on facebook.


About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →