Only retrieve validated data from $request->validate()

Published 2 months ago by Cruorzy

So sometimes I call my validation after a few checks, so i have a FormRequest with the rules and made a static function in there that return the rules.

But when I do

$data = $request->validate(CostCreate::getRules());
return $data;

I get more data than expected, I expect only the validate data but instead I get ALL the columns.

Any idea how I get only the validated data?

Best Answer (As Selected By Cruorzy)
click

See this ticket on SO https://stackoverflow.com/questions/50317448/how-to-validate-an-array-in-laravel-and-return-its-filtered-values

Same question in 2013, no solution provided. And there still is no solution (not by default at least).

protected function extractInputFromRules(Request $request, array $rules)
    {
        return $request->only(collect($rules)->keys()->map(function ($rule) {
            return Str::contains($rule, '.') ? explode('.', $rule)[0] : $rule;
        })->unique()->toArray());
    }

If you have nested data there is no whitelist on fieldnames. If you want that you should do it manually.

A simple solutions could be to use array_only($inputArray, ['name', 'type', 'amount', 'price']) on each array element. Another solution would be is to extend/overwrite the behavior of above method in the ValidatesRequests trait.

click
click
2 months ago (77,740 XP)

That should work.

Simple test case. If you place this somewhere in a controller and go to: your-page?foo=bar&otherfield=notalllowed what do you see?

$data = request()->validate([
   'foo' => 'nullable',
]);

dd($data);
Cruorzy

That actually works, but when doing a axios.post and return $data after the validation. it includes everything.

Note : its a array

return [
    '*.name' => 'required|max:150',
    '*.type' => 'required',
    '*.amount' => 'required|integer',
    '*.price' => 'required|integer',
];

click
click
2 months ago (77,740 XP)

See this ticket on SO https://stackoverflow.com/questions/50317448/how-to-validate-an-array-in-laravel-and-return-its-filtered-values

Same question in 2013, no solution provided. And there still is no solution (not by default at least).

protected function extractInputFromRules(Request $request, array $rules)
    {
        return $request->only(collect($rules)->keys()->map(function ($rule) {
            return Str::contains($rule, '.') ? explode('.', $rule)[0] : $rule;
        })->unique()->toArray());
    }

If you have nested data there is no whitelist on fieldnames. If you want that you should do it manually.

A simple solutions could be to use array_only($inputArray, ['name', 'type', 'amount', 'price']) on each array element. Another solution would be is to extend/overwrite the behavior of above method in the ValidatesRequests trait.

Snapey
Snapey
2 months ago (1,036,705 XP)

what laravel version?

Cruorzy

@Snapey 5.6.29

@click alright thanks for that info! bit of a bummer

Please sign in or create an account to participate in this conversation.