Laravel 8 Custom Validation Rule Example

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in blog, I will show you how to create custom validation rule in laravel 8 application. Now here i will explain laravel custom validation rule example. We know that custom validation rule is very useful and interesting concept of laravel. Because of custom validation through we can re-use the validation and also use it easily by just name as like as laravel core.

So now in this example, i am going to show you how to create a custom validation rules in laravel application. Also we will know how can we create it very simple way and use too. So we can create custom validation rules by using few following step and then we can see preview of example on that give for add custom validation rule. So now In this example we will create is_even_string
custom validation rule.

Just need to following steps of custom validation rule in laravel application

Step 1 : Create Route

At in the first step we need to create two route for example, one for get method will help us to generate view and then second one for post method that help us for from submit

following path:/routes/web.php

Route::get('customValiRule', 'CustomValRuleController@customValiRule');
Route::post('customValiRulePost', 'CustomValRuleController@customValiRulePost');
Step 2 : Create Controller

Now here in this step we need to create new controller as CustomValRuleController, So need to run bellow command for generate a new controller

php artisan make:controller CustomValRuleController

So now in this step, this controller i will manage custom validation bellow content in controller file. Just need to following file path

following path:/app/Http/Controllers/CustomValRuleController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CustomValRuleController extends Controller
{
    public function customValiRule()
    {
        return view('customRuleVali');
    }
    public function customValiRulePost(Request $request)
    {
        $this->validate($request, [
            'title' => 'required|is_even_string',
        ]);

        print_r('done');
    }
}
Step 3: Declare Validation

So in this step i will declare and write code of my custom validation. Now at first open app/Providers/AppServiceProvider.php file and then put following code:

following path:/app/Providers/AppServiceProvider.php

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Schema;
use Validator;

class AppServiceProvider extends ServiceProvider
{
    /**
     * Register any application services.
     *
     * @return void
     */
    public function boot()
    {
        Validator::extend('is_even_string', function($attribute, $value, $parameters, $validator) {
            if(!empty($value) && (strlen($value) % 2) == 0){
                return false;
            }
                return true;
        });
    }

    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

So after,we need to give error message for new created custom validation rules, Now need register custom message open resoueces/lang/en/validation.php and then put one line as i did bellow:

following path:/resoueces/lang/en/validation.php

return [
    'is_even_string' => "The :attribute must be odd string lenght.",
    'accepted' => 'The :attribute must be accepted.'
    .....
]
Step 4: Create View

So now we are in last step, now we need to create one view for demo. Now just copy bellow code and put on following file.

following path:/resources/views/customRuleVali.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 6 Custom Validation Rule Example - CodingsPoint.com </title>
    <link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.4.1/css/bootstrap.css">
</head>
<body class="bg-dark">
<div class="container">
    <div class="row">
        <div class="col-md-8 offset-md-2">
            <div class="card mt-5">
                <div class="card-header">
                    <h5>Laravel 6 Custom Validation Rule Example - CodingsPoint.com </h5>
                </div>
                <div class="card-body">
                    <form action="{{ URL::to('customValiRulePost') }}" method="post">
                        @csrf
                        @if (count($errors) > 0)
                            <div class="alert alert-danger">
                                <ul>
                                    @foreach ($errors->all() as $error)
                                        <li>{{ $error }}</li>
                                    @endforeach
                                </ul>
                            </div>
                        @endif
                        <div class="row">
                            <div class="col-md-12">
                                <div class="form-group">
                                    <label>Title<span class="text-danger">*</span></label>
                                    <input type="text" name="title" class="form-control" placeholder="Enter title">
                                </div>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-12">
                                <button class="btn btn-success">Submit</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
</body>
</html>

Hope now we are ready to run our example so run bellow command for quick run:

php artisan serve

Now you can open bellow URL on your browser:

http://localhost:8000/customValiRule

Read Also : PHP AngularJS CRUD with Search and Pagination Example From Scratch

thank you for read .I hope it can help you.Also you can follow us on Facebook.

About code chef

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 code chef →