How do I get ONLY the validated data from a laravel FormRequest?

How do I get ONLY the validated data from a laravel FormRequest?

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

Stefan Bogdanescu

Founder & Senior Architect

Extracting Validated Payloads from Laravel FormRequests

When building APIs or handling forms, you often define strict validation rules in a FormRequest class to ensure data integrity before processing. Once that request passes through the validator and enters your controller method via constructor injection, you might find that $request->all() still contains unvalidated fields like CSRF tokens, hidden form inputs, or query string parameters that were never part of your business logic.

In your specific scenario, the PlanRequest validates a set of fees and organization details, but the resulting array includes 'foo' => 'bar'. You are looking for a way to extract only the fields that passed validation without manually listing every single property name in an only() call.

Laravel does not provide a built-in method like getValidatedOnly() because the underlying validator object is separate from the input collection. However, there are two distinct approaches depending on whether you need the raw validated values or the typed properties themselves.

Using the Validator Instance

The most direct way to access only the data that passed validation is to inspect the underlying Illuminate\Validation\Validator instance. When a FormRequest is resolved by Laravel's service container, it injects the request into your controller. Inside that request object, you can access the validator via $this->validator.

Since you are inside a controller method receiving the $request, you must first access the validator property on that request object. You can then call the get() method with an empty array or iterate through the validated data. However, the most robust pattern is to access the validated data directly if your rules define a specific schema, or by checking the passed validation state.

If you strictly need the subset of data defined in your rules(), you can leverage the fact that the validator knows which attributes exist in the passed dataset versus those that failed. But since your goal is to exclude fields like 'foo' entirely, the standard Laravel approach involves accessing the validated array after a successful pass.

To get just the validated keys programmatically without hardcoding them, you can access the $validated property if you are within the FormRequest itself, or use the validator's methods in the controller. A common pattern in controllers is to check if validation passed and then access the specific data:

public function store(PlanRequest $request)
{
    // If FormRequest extends Illuminate\Foundation\Http\FormRequest
    // It usually handles validation automatically based on rules() or validate() method.
    
    // Accessing the validated array directly via the request instance
    // Note: In older Laravel versions, you might need to check the validator property.
    // In modern Laravel (8.x+), FormRequest often exposes validated data differently depending on usage.
    
    // The cleanest way without manual listing is to use the validator's passed attributes logic
    // or simply trust that the request object has a 'validated' property if configured correctly.
    
    // However, strictly speaking, FormRequest validates against rules(). 
    // If you want ONLY the keys defined in rules(), you can do this:
    
    $validator = $request->validator();
    $passedAttributes = array_keys($validator->getPassed());
    
    // This gives you an array of keys that passed validation.
    // You can then filter the original request input based on these keys.
    
    $validatedData = [];
    foreach ($passedAttributes as $key) {
        if (array_key_exists($key, $request->all())) {
            $validatedData[$key] = $request->all()[$key];
        }
    }

    return response()->json($validatedData);
}

The method $request->validator() returns the Illuminate\Contracts\Validation\Validator instance. Calling getPassed() on this object returns an array of keys that satisfied all validation rules. By iterating over these keys, you can reconstruct a dataset containing only what is valid, effectively filtering out garbage data like 'foo'.

The Trade-Off: Dynamic vs. Static Typing

While the approach above works dynamically, it introduces complexity to your controller logic. You are essentially rebuilding the array in memory. If your rules() method changes frequently, you risk desync between what passes validation and how you structure the response.

A more idiomatic Laravel solution is often to define a specific property in the controller or use type hints that align with your validated data. Alternatively, if you are using Laravel 8 or higher, you can leverage the validate() method within the FormRequest itself to explicitly define what should be returned:

class PlanRequest extends FormRequest
{
    public function rules()
    {
        return [
            'name' => 'required|string|min:3|max:191',
            'monthly_fee' => 'required|numeric|min:0',
            // ... other rules
        ];
    }

    public function validated(array $except = [])
    {
        return parent::validated($except);
    }
}

In this scenario, accessing $request->validated() inside the controller will return an array containing only the keys defined in your rules() method. This effectively filters out any extra garbage data automatically. This is the cleanest, most maintainable solution because it decouples the validation logic from the payload construction logic.

Why Not Just Use $request->all()?

The reason $request->all() includes 'foo' is that Laravel's request object represents the raw HTTP input stream. It captures everything sent by the client (body, query string, headers) regardless of whether you intend to use it. Validation acts as a gatekeeper, but the original collection remains intact until explicitly filtered.

By using $request->validated(), you are asking the framework to return the subset of data that successfully passed the rules defined in your FormRequest. This ensures that your subsequent business logic never accidentally processes unvalidated or unexpected fields, reducing the surface area for bugs and security vulnerabilities.

Our network