How To Create Custom Validation In Laravel 10

Creating custom validation rules in Laravel allows you to define your own rules for validating input data in your application. In this guide, we’ll walk through the process of creating a custom validation rule in Laravel step by step.

To create a custom validation rule in Laravel, follow these steps:

Step 1: Create the Rule Class

The first step is to create a custom rule class that extends the Illuminate\Contracts\Validation\Rule interface. This interface requires you to implement two methods: passes and message.

The passes method is responsible for performing the validation logic. It receives two parameters: the attribute being validated and its corresponding value. Inside this method, you can implement the logic to determine whether the validation passes or fails. The method should return true if the validation passes and false if it fails.

How To Create Custom Validation Rule In Laravel 10

The message method is used to define the error message that will be displayed if the validation fails. This method should return the error message as a string.

Let’s create a custom rule class to validate that a given input is a multiple of a specified number. Create a new file called MultipleOfRule.php in the app/Rules directory (you may need to create the Rules directory if it doesn’t exist), and add the following code:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class MultipleOfRule implements Rule
{
    protected $multiple;

    public function __construct($multiple)
    {
        $this->multiple = $multiple;
    }

    public function passes($attribute, $value)
    {
        return $value % $this->multiple === 0;
    }

    public function message()
    {
        return "The :attribute must be a multiple of {$this->multiple}.";
    }
}

In this example, the MultipleOfRule class takes a parameter $multiple in its constructor, which represents the number that the input should be a multiple of. The passes method checks whether the input value is divisible by $multiple without a remainder. If it is, the validation passes; otherwise, it fails. The message method returns the error message with a placeholder :attribute that will be replaced with the attribute name during validation.

Step 2: Register the Custom Rule

To use the custom validation rule, you need to register it with Laravel’s validation system. Open the App\Providers\AppServiceProvider class, located in app/Providers, and add the following code to the boot method:

use Illuminate\Support\Facades\Validator;
use App\Rules\MultipleOfRule;

public function boot()
{
    Validator::extend('multiple_of', function ($attribute, $value, $parameters, $validator) {
        $multiple = $parameters[0] ?? 1;
        $rule = new MultipleOfRule($multiple);
        return $rule->passes($attribute, $value);
    });

    Validator::replacer('multiple_of', function ($message, $attribute, $rule, $parameters) {
        $multiple = $parameters[0] ?? 1;
        return str_replace(':multiple', $multiple, $message);
    });
}

In the above code, we use the Validator::extend method to register a new validation rule named multiple_of. The closure passed to Validator::extend creates an instance of the MultipleOfRule class and calls the passes method to perform the validation. The closure receives the attribute being validated, its value, any additional parameters specified in the rule, and the validator instance.

Additionally, we use the Validator::replacer method to customize the error message. The closure passed to `Validator::replacer

All the best nerd!

See our previous post

Leave a Reply