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

3

25

Lets say I have the following Custom Request:

class PlanRequest extends FormRequest
{
    // ...


    public function rules()
    {

        return
        [
            'name'              => 'required|string|min:3|max:191',
            'monthly_fee'       => 'required|numeric|min:0',
            'transaction_fee'   => 'required|numeric|min:0',
            'processing_fee'    => 'required|numeric|min:0|max:100',
            'annual_fee'        => 'required|numeric|min:0',
            'setup_fee'         => 'required|numeric|min:0',
            'organization_id'   => 'exists:organizations,id',
        ];
    }
}

When I access it from the controller, if I do $request->all(), it gives me ALL the data, including extra garbage data that isn't meant to be passed.

public function store(PlanRequest $request)
{
    dd($request->all());
    // This returns
    [
        'name'              => 'value',
        'monthly_fee'       => '1.23',
        'transaction_fee'   => '1.23',
        'processing_fee'    => '1.23',
        'annual_fee'        => '1.23',
        'setup_fee'         => '1.23',
        'organization_id'   => null,
        'foo'               => 'bar', // This is not supposed to show up
    ];
}

How do I get ONLY the validated data without manually doing $request->only('name','monthly_fee', etc...)?

Mccrary answered 22/12, 2017 at 5:36 Comment(0)
O
47

$request->validated() will return only the validated data.

Example:

public function store(Request $request)
{
    $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

    $validatedData = $request->validated();

}

Alternate Solution:

$request->validate([rules...]) returns the only validated data if the validation passes.

Example:

public function store(Request $request)
{
   
    $validatedData = $request->validate([
        'title' => 'required|unique:posts|max:255',
        'body' => 'required',
    ]);

}
Overstrain answered 22/12, 2017 at 5:45 Comment(3)
I figured this out, but I'm going to upvote yours cuz you beat me to the punch. :)Mccrary
There should be a way to do the same without FormRequestSelfsatisfied
Lravel 9 $validated = $request->safe();Forbis
M
9

OK... After I spent the time to type this question out, I figured I'd check the laravel "API" documentation: https://laravel.com/api/5.5/Illuminate/Foundation/Http/FormRequest.html

Looks like I can use $request->validated(). Wish they would say this in the Validation documentation. It makes my controller actions look pretty slick:

public function store(PlanRequest $request)
{
    return response()->json(['plan' => Plan::create($request->validated())]);
}
Mccrary answered 22/12, 2017 at 5:46 Comment(0)
D
3

This may be an old thread and some people might have used the Validator class instead of using the validator() helper function for request.

To those who fell under the latter category, you can use the validated() function to retrieve the array of validated values from request.

$validator = Validator::make($req->all(), [
    // VALIDATION RULES
], [
    // VALIDATION MESSAGE
]);

dd($validator->validated());

This returns an array of all the values that passed the validation.

This only starts appearing in the docs since Laravel 5.6 but it might work up to Laravel 5.2

Donelson answered 16/1, 2023 at 7:0 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.