Learn how to implement Laravel 12 custom validation rules with parameters in a simple, structured way. This tutorial covers multiple approaches, including Rule objects and Closures, with hands-on examples.
Why Use Custom Validation Rules?
Sometimes Laravel’s built-in validation rules aren’t enough. That’s when custom validation rules come in handy. They are especially useful when:
- You need to handle complex business logic.
- Validation depends on multiple fields.
- You require domain-specific rules that Laravel doesn’t include out of the box.
Custom rules with parameters also help you avoid code duplication by creating a single reusable class. These classes can be adapted for different contexts by passing parameters through the constructor.
Creating Custom Validation Rules with Parameters
Laravel offers two main approaches for building custom validation rules:
- Using a Closure
- Using a Rule Object (recommended)
In this guide, we’ll explore both methods to implement parameterized validation rules.
Method 1: Using Rule Objects (Recommended)
The Rule Object approach is the preferred method when you want to reuse validation logic across multiple forms, controllers, or requests.
Step 1: Generate a Custom Rule
Use Artisan to generate a new rule class:
php artisan make:rule Username
This command creates a new file Username.php inside the App\Rules directory.
Step 2: Update AppRulesUsername.php
Next, open the generated Username.php file and update it with the following code:
<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
class Username implements ValidationRule
{
protected $minLength;
protected $allowSpecialChars;
public function __construct(int $minLength = 3, bool $allowSpecialChars = false)
{
$this->minLength = $minLength;
$this->allowSpecialChars = $allowSpecialChars;
}
/**
* Run the validation rule.
*
* @param \Closure(string, ?string=): \Illuminate\Translation\PotentiallyTranslatedString $fail
*/
public function validate(string $attribute, mixed $value, Closure $fail): void
{
// Check minimum length
if (strlen($value) < $this->minLength) {
$fail("The {$attribute} must be at least {$this->minLength} characters long.");
return;
}
// Check for valid characters
if ($this->allowSpecialChars) {
$pattern = '/^[a-zA-Z_@\-\.]+$/';
} else {
$pattern = '/^[a-zA-Z_]+$/';
}
if (!preg_match($pattern, $value)) {
$fail("The {$attribute} contains invalid characters.");
return;
}
}
}
In Laravel 12, you can implement the validate(string $attribute, mixed $value, Closure $fail) method via the ValidationRule interface, while also accepting constructor parameters for flexible validation policies.
In the following custom rule example, we’ll define two parameters in the constructor:
- minLength – sets the minimum number of characters required (default: 3).
- allowSpecialChars – determines whether special characters are permitted (default: false).
Both parameters are optional, so you can use the defaults or override them when applying the rule.
Here’s how the validation logic works:
- Check minimum length – If the username does not meet the required length, a validation error is returned.
- Check allowed characters – If allowSpecialChars is true, special characters like @, -, or . are allowed. If allowSpecialChars is false, only letters and underscores are permitted.
This approach makes your validation logic **configurable and reusable **across multiple forms or controllers.
Check out the full step-by-step guide here : Laravel 12 Custom Validation Rule with Parameters
Top comments (0)