Creating Custom Validation Rules in Laravel 11 - TechvBlogs

Creating Custom Validation Rules in Laravel 11

Enforce specific requirements in your Laravel 11 application by creating custom validation rules. Learn how to tailor validation to your app's needs.


Suresh Ramani - Author - TechvBlogs
Suresh Ramani
 

1 month ago

TechvBlogs - Google News

Laravel 11's validation system safeguards data integrity by enforcing rules on submitted form data. While built-in rules address many common scenarios, unique validation needs may arise. This guide delves into crafting custom validation rules in Laravel 11, empowering you to tailor validation to your application's specific requirements.

Creating Custom Validation Rules in Laravel 11

Generating the Rule Class

Utilize the Laravel Artisan command to swiftly generate a new custom validation rule class:

php artisan make:rule StrongPassword

This command creates a class named StrongPasswordRule.php within the app/Rules directory.

Implementing the Custom Rule

Open the generated StrongPasswordRule.php class and define the custom validation logic:

<?php

namespace App\Rules;

use Illuminate\Contracts\Validation\Rule;

class StrongPasswordRule implements Rule
{
    /**
     * Determine if the value is valid.
     *
     * @param  string  $attribute
     * @param  mixed  $value
     * @return bool
     */
    public function passes($attribute, $value)
    {
        return strlen($value) >= 8 && preg_match('/(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]/', $value);
    }

    /**
     * Get the validation error message for the rule.
     *
     * @param  string  $attribute
     * @return string
     */
    public function message($attribute)
    {
        return 'The :attribute must be at least 8 characters and include at least one uppercase letter, lowercase letter, number, and special character.';
    }
}
  • The passes method houses the custom validation logic. In this example, it enforces a minimum password length and the presence of various character types.
  • The message method defines the error message displayed when validation fails.

Utilizing the Custom Rule

Within your form request class, incorporate the custom validation rule in the rules method:

public function rules()
{
    return [
        'password' => ['required', 'confirmed', new StrongPasswordRule],
    ];
}
  • Here, the StrongPasswordRule is applied to the password field, ensuring it adheres to the defined criteria.

Leveraging the Validation Request

In your controller method, associate the form request class with the request:

public function store(StoreUserRequest $request)
{
    // ... store user data ...
}
  • This binds the validation rules from the StoreUserRequest class to the incoming request, automatically triggering validation when the form is submitted.

By following these steps, you've successfully created and implemented a custom validation rule in Laravel 11, strengthening your application's data integrity and user experience. Remember to tailor the validation logic within the passes method to match your specific requirements.

Comments (0)

Comment


Note: All Input Fields are required.