How to Dynamically Validate All Fields in Laravel 11

In Laravel, validating user inputs is essential, but sometimes you may need to skip validation for certain fields. If you have many fields, manually writing validation rules for each one can be time-consuming.

In this guide, I'll show you how to dynamically validate all fields except for a few, making your code cleaner and easier to manage.

How to Dynamically Validate All Fields in Laravel 11

How to Dynamically Validate All Fields in Laravel 11

 

Example:

To validate all fields in the request while excluding specific fields, you can use Laravel's Validator class and the except method.

use Illuminate\Support\Facades\Validator;

public function validateRequest(Request $request)
{
    // Define validation rules for all fields
    $rules = [
        'name' => 'required|string|max:255',
        'email' => 'required|email|max:255',
        'age' => 'nullable|integer|min:18',
        'field_to_skip' => 'nullable',
        'another_field_to_skip' => 'nullable',
    ];

    // Exclude specific fields from validation
    $filteredData = $request->except(['field_to_skip', 'another_field_to_skip']);

    // Create a Validator instance
    $validator = Validator::make($filteredData, $rules);

    // Check validation
    if ($validator->fails()) {
        return response()->json([
            'status' => 'error',
            'errors' => $validator->errors(),
        ], 422);
    }

    return response()->json(['status' => 'success', 'message' => 'Validation passed!']);
}

 

Example:

If you have many fields (e.g., 100), and you want to validate all fields except a few, manually defining all validation rules is inefficient. Instead, you can dynamically build the validation rules by iterating over the request data and excluding the fields you don't want to validate.

use Illuminate\Support\Facades\Validator;

public function validateRequest(Request $request)
{
    // Fields to exclude from validation
    $excludeFields = ['field_to_skip', 'another_field_to_skip'];

    // Dynamically generate validation rules for all fields except excluded ones
    $validationRules = collect($request->all())
        ->keys() // Get all field names
        ->reject(fn($field) => in_array($field, $excludeFields)) // Remove excluded fields
        ->mapWithKeys(fn($field) => [$field => 'required|string|max:255']) // Assign rules
        ->toArray();

    // Perform validation
    $validator = Validator::make($request->all(), $validationRules);

    if ($validator->fails()) {
        return response()->json([
            'status' => 'error',
            'errors' => $validator->errors(),
        ], 422);
    }

    return response()->json(['status' => 'success', 'message' => 'Validation passed!']);
}

 


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