Part #2: How to use Laravel's Validator::extend method for custom validation

Harish Kumar · · 7493 Views

Validation is important in any application as it validates a form before performing actions on it. It allows the user to know their input is accurate and confident about the operation they are about to do.

There are many ways to validate form requests in Laravel. One of them is closure-based validation which I have already explained it. You can read about it from the below article link:

  1. Part #1: Closure-based Laravel Validation

In this article, I will show how easily Laravel handles custom validation using the Validator::extend method. It allows us to register custom validation rules globally. It allows two different ways to extend custom validation using a validator.

The first is passing a closure to the extend method on the class. It is a good option when your project is small and you want to keep the logic together.

The second most preferred way is to pass in the name of the class instead of the closure function. It means you have to extend the validator in one place and keep the logic for validation in a separate class.

Let’s look at setting up both of these methods!

Extending the Validator using closure

As I mentioned above, here we pass a closure to the extend method on the validator class:

Validator::extend('match_current_password', function($attribute, $value, $parameters, $validator) {
    if (!auth()->check()) {
        return false;
    }

    return Hash::check($value, auth()->user()->password);
});

The first argument of the extend method is the name of the validation rule and the second is the closure.

The closure accepts three arguments:

$attribute: The name of the attribute that you are validating.

$value: The value of the attribute

$parameters: An array of parameters that can be passed to the rule.

Extending the Validator using class

Extending the validator using closure is nice and easy, but resolving from a class is much prefered. What are the benefits of resolving from a class? Well, I think there are two reasons for this:

  1. Easier to manage multiple extensions

  2. Always extend in a consistent way

So the first thing to do is to create your class for validation logic:

<?php

namespace App\Validator;

use Illuminate\Support\Facades\Hash;

class MatchCurrentPassword
{
    public function validate($attribute, $value, $parameters, $validator)
    {
        if (! auth()->check()) {
            return false;
        }

        return Hash::check($value, auth()->user()->password);
    }
}

Now pass, this class in the validator extend method:

Validator::extend('match_current_password', MatchCurrentPassword::class);
0

Please login or create new account to add your comment.

0 comments
You may also like:

Building a Real-Time Chat App with Laravel Reverb and Nuxt 3

Building a real-time chat application is a great way to understand the power of WebSockets and real-time communication. In this tutorial, we will walk through creating a Real-Time (...)
Harish Kumar

How to Set Up Nuxt 3 Authentication with Laravel Sanctum (Step-by-Step Guide)

In modern web development, securing your application’s authentication process is a top priority. For developers building Single Page Applications (SPA) or Server-Side Rendered (...)
Harish Kumar

Laracon US 2024: Laravel 11 Minor Features That Enhance Performance

At Laracon US 2024, Taylor Otwell and the Laravel team introduced a series of "minor" features for Laravel 11 that are anything but minor. These enhancements, while not headline-grabbing (...)
Harish Kumar

PHP OPCache: The Secret Weapon for Laravel Performance Boost

OPCache, a built-in PHP opcode cache, is a powerful tool for significantly improving Laravel application speed. This guide will demonstrate how to effectively utilize OPCache to (...)
Harish Kumar

How to Use DTOs for Cleaner Code in Laravel, Best Practices and Implementation Guide

When developing APIs in Laravel, ensuring your responses are clear, concise, and consistent is crucial for creating a maintainable and scalable application. One effective way to (...)
Harish Kumar

Data Type Validation in Laravel Collections with the `ensure()` Method

Before moving on to the ensure() method, let us first know what Laravel Collections actually are. These are wrappers of PHP arrays, offering a fluent and helpful interface in interacting (...)
Harish Kumar