How to Create Dynamic Validation Rule in Laravel 12

When I was working on a project recently, I realized I needed a dynamic way to validate user inputs based on different conditions. Laravel 12 makes it super easy to create custom validation rules! Let me show you a simple and clear method to build your own dynamic validation rule in Laravel 12.

Step-by-Step Guide: Creating a Dynamic Validation Rule in Laravel 12

Laravel Validation Example

 

Step 1: Create a Custom Rule Class

Run this artisan command to generate the rule:

php artisan make:rule DynamicValidationRule

It will create a new file in app/Rules/DynamicValidationRule.php

 

Step 2: Define Your Logic Inside the Rule Class

Open the generated file and modify it like this:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class DynamicValidationRule implements Rule
{
    protected $parameter;

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

    public function passes($attribute, $value)
    {
        // Example: Validate based on parameter
        return strlen($value) > $this->parameter;
    }

    public function message()
    {
        return 'The :attribute must be longer than ' . $this->parameter . ' characters.';
    }
}

 

Step 3: Use the Rule in Your Form Request or Controller

Example in your controller:

use App\Rules\DynamicValidationRule;

$request->validate([
    'username' => ['required', new DynamicValidationRule(5)],
]);

This rule will ensure the username is longer than 5 characters.

 

Step 4: Make It More Dynamic

You can even pass different conditions dynamically by modifying the constructor to accept more parameters.

new DynamicValidationRule($request->input('min_length'));

 

Conclusion

Creating dynamic validation rules in Laravel 12 is a powerful way to keep your code clean and reusable. With just a few lines, you can handle complex validations easily! Next time you need custom rules, just create your own class and control the logic yourself.

 

Frequently Asked Questions

  1. What is a validation rule in Laravel 12?

    A validation rule in Laravel 12 ensures that user inputs meet specific criteria before saving or processing them.

  2. How do I create custom validation rules in Laravel 12?

    You can create custom validation rules using the php artisan make:rule RuleName command and defining your logic inside the rule.

  3. Can validation rules in Laravel 12 be dynamic?

    Yes! Laravel 12 allows dynamic rules by passing parameters to your custom rule classes.

  4. Is it better to use custom rules or inline validation in Laravel 12?

    For complex scenarios, custom rules are better because they keep your controller clean and your logic organized.

  5. Where are custom validation rules stored in Laravel?

    By default, custom rules are stored in the app/Rules directory.

 

For more detailed information, refer to the official Laravel documentation on validation.


You might also like:

techsolutionstuff

Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of techsolutionstuff.com. Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.

RECOMMENDED POSTS

FEATURE POSTS