How add Custom Validation Rules when using Form Request Validation in Laravel 5

How add Custom Validation Rules when using Form Request Validation in Laravel 5

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Implementing Custom Validation Rules in Laravel Form Requests

You can extend Laravel's validation system to include your own rules, but the implementation differs significantly depending on whether you are working within a FormRequest class or directly using the Validator facade. The confusion often stems from mixing these two distinct entry points. When defining rules inside a FormRequest, you must register your custom validator logic globally before it can be referenced by name in the $rules array.

Your current approach defines the rule numeric_array within the request class, but the registration of that rule is missing. The code snippet you provided uses Validator::extend() to create a closure that checks if every item in the array is an integer. This registration must happen at the application level, typically inside the AppServiceProvider.

To make this work with your Form Request, you need to ensure the custom validator is registered before any validation logic runs. In Laravel 5, the standard location for this is the register method of your AppServiceProvider. You would place the following code there:

public function register()
{
    Validator::extend('numeric_array', function($attribute, $value, $parameters) {
        foreach ($value as $v) {
            if (!is_int($v)) {
                return false;
            }
        }
        return true;
    });
}

Once this registration is in place, your FormRequest class can simply reference the rule by its registered name. You update the $rules property of your request to match the exact key used during registration:

protected $rules = [
    'shipping_country' => ['max:60'],
    'items' => ['array', 'numeric_array']
];

It is crucial to note that the rule name in the array must match the string passed to Validator::extend(). If you register the validator as numeric_array but reference it as numericarray, Laravel will throw a validation exception indicating that the specified validation rule does not exist. This mismatch is a common pitfall when transitioning from direct validation calls to Form Request structures.

Additionally, consider how your custom closure handles edge cases. The logic provided in your source material assumes $value is always an array. If the input is null or empty, the foreach loop simply won't execute, and the function will return true. Depending on your business logic, you might want to explicitly check if the array has items before iterating, perhaps requiring at least one integer value. For instance, adding a required rule alongside this custom check ensures the field is present before the type validation runs.

Another consideration involves error messages. While not strictly necessary for the code to function, defining a custom error message improves developer experience. You can pass an array of parameters to the validator, but more commonly, you might define a custom error message in your language file (e.g., en/validation.php) or use the $parameters argument if you need dynamic messages based on specific configuration passed during registration.

Finally, remember that global registration means this rule is available across the entire application. If you were building a multi-tenant system or needed to isolate validation rules per environment, you might consider defining these within the Form Request itself using Validator::make(...)->extend(), though that approach is less common and requires instantiating the validator instance explicitly. Stick to the global registration for clean, reusable logic unless specific isolation is required.

Our network