DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for How to Create Custom Validation Rules in Laravel 9
Suresh Ramani
Suresh Ramani

Posted on • Originally published at techvblogs.com

How to Create Custom Validation Rules in Laravel 9

The biggest advantage of Laravel over other frameworks is the fact that it comes with a lot of built-in features. In this article, we are going to understand Laravel Custom Validation Rules.

Laravel provides you with a rich set of validation rules which you can add to validate the requests. What is good about the framework is not only that it gives a bunch of built-in features but also allows you to extend its functionality and build your own set of rules, that you may get to use in more than one project.

In this article, we will be exploring how to create Custom Validation Rule in Laravel 9.

In most instances, the rules provided by Laravel are sufficient enough to validate all the use cases, but in certain conditions, you might need to add custom validation rules. Let’s dive deep and take a look into how we can define custom rules and enable them to validate our requests.

Create a Form Request

It is a good practice to encapsulate your validation rules in the form request object.

php artisan make:request UserRequest
Enter fullscreen mode Exit fullscreen mode
<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class UserRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, mixed>
     */
    public function rules()
    {
        return [
            'name'=>'required|max:255',
            'email'=>'required|email|unique:users',
            'birth_year'=>'required'
        ];
    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see there are two methods that are already present in the class:

  • Authorize method: It is used to define the authorization rules. It can help you in determining who all has access to these pages.
  • Rules method: This is the method where you would be adding your validation rules. We are validating name, email, and Birth Year.

Creating Rule Class

As we have just seen, we have created a Customer Model that has a name and email. Now let us add some rules to validate these fields. You can execute the below command to create a custom rule.

php artisan make:rule BirthYearRule
Enter fullscreen mode Exit fullscreen mode
<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class BirthYearRule implements Rule
{
    /**
     * Create a new rule instance.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Determine if the validation rule passes.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return $value >= 1990 && $value <= date('Y');
    }

    /**
     * Get the validation error message.
     *
     * @return string
     */
    public function message()
    {
        return "The :attribute must be between 1990 to ".date('Y').".";
    }
}
Enter fullscreen mode Exit fullscreen mode

As you can see there are two functions that we need to implement in the above class. These are passes() and message().

Adding Validation Logic

Once we have added the validation rule class, let us add the validation logic also. For this example, lets us consider that the birth year is greater than or equal to 1990 and less than or equal to the current year. So we will go ahead and update our passes function.

/**
* Determine if the validation rule passes.
*
* @param  string  $attribute
* @param  mixed  $value
* @return bool
*/
public function passes($attribute, $value)
{
    return $value >= 1990 && $value <= date('Y');
}
Enter fullscreen mode Exit fullscreen mode

As a good coding practice, we should update the message() function. In the message() function, we are specifying the message that would be shown, in case the validation error happens.

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
    return "The :attribute must be between 1990 to ".date('Y').".";
}
Enter fullscreen mode Exit fullscreen mode

Using the Validation Rule in Form Request Class

Now that we have created the validation rule, let us go ahead and add the validation in the form request class which we had added above.

The first step would be to import this newly created class into our request class. We can import using the below code:

use App\Rules\BirthYearRule;
Enter fullscreen mode Exit fullscreen mode

Now that we have imported the class, let us go ahead and update and update the rules() method.

/**
* Get the validation rules that apply to the request.
*
* @return array<string, mixed>
*/
public function rules()
{
    return [
        'name'=>'required|max:255',
        'email'=>'required|email|unique:users',
        'birth_year'=>['required', new BirthYearRule]
    ];
}
Enter fullscreen mode Exit fullscreen mode

As you can see, we now have added an extra validation in the name rules list. We are creating a new instance of the BirthYearRule class. When the rules() method will get executed, the BirthYearRule class will be instantiated and it will call the passes() method in the BirthYearRule class to validate the name.

Using the Validation Rule in Controller

The above method of adding the rule in the form request is useful when you are creating the request object. The rules class can also be added directly to the controller code. Let us quickly see how we can implement the BirthYearRule class directly in the controller.

Again, the first step would be to import the BirthYearRule directly into the controller class. We can use the below code to directly import the class.

use App\Rules\BirthYearRule;
Enter fullscreen mode Exit fullscreen mode

Once you have imported the class, the next step would be adding in the validate call.

$request->validate([
    'name'=>'required|max:255',
    'email'=>'required|email|unique:users',
    'birth_year'=>['required', new BirthYearRule]
])
Enter fullscreen mode Exit fullscreen mode

As you can see in the request validate function, we added a new rule in name validation. We are creating a new object of the BirthYearRule class. During this call, a new object of BirthYearRule will be created, and validation will take place.

Custom Validation Rule Using Closures

Now that we have seen how we can create our custom validation rules using the make:rule command, we should understand how we can create commands using closures. The concept of the validation rule remains the same, it is just implemented in a different form.

$validator = Validator::make($request->post(),[
    'birth_year'=>[
        'required',
        function($attribute, $value, $fail){
            if($value >= 1990 && $value <= date('Y')){
                $fail("The :attribute must be between 1990 to ".date('Y').".");
            }
        }
    ]
]);
Enter fullscreen mode Exit fullscreen mode

As we can see in the above code, we are using the Validator class and directly making the rule. The function in it is getting 3 values: attribute, value, and fail. The attribute is the field for which the validation is happening. The value corresponds to the actual value of the said object and failure is the callback method that would be executed once the validation fails.

Thank you for reading this blog.

Read Also: SPA Authentication using Laravel 9 Sanctum, Vue 3 and Vite

If you want to manage your VPS / VM Server without touching the command line go and Checkout this link. ServerAvatar allows you to quickly set up WordPress or Custom PHP websites on VPS / VM in a matter of minutes. You can host multiple websites on a single VPS / VM, configure SSL certificates, and monitor the health of your server without ever touching the command line interface.

Top comments (0)

🌚 Life is too short to browse without dark mode