Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

binarymelon's avatar

Root Array Validation

I'm just wondering how I can create a validator for a request where the body root is an array. It seems like if the rules are '*.' it also applies to the query parameters.

0 likes
6 replies
Sinnbeck's avatar

Why would the root be an array? How are you posting the data? Why would it need to be at the root level?

Sinnbeck's avatar

@binarymelon and the request comes from an external server or? Sending data without a key is a really bad idea in general so if it's you own code, you should just give it a key

Sinnbeck's avatar

Anyways. What you can probably do is something like this

$data = request()->except(['_token']); //add any you want to exclude
$validator = Validator::make($data, [
            '*' => 'required|max:255',
        ]);
 
        if ($validator->fails()) {
            return redirect('post/create')
                        ->withErrors($validator)
                        ->withInput();
        }
binarymelon's avatar

@Sinnbeck Not really interested in modifying the contract here. just want to know of if Laravel will support this scenario.

Sinnbeck's avatar

@binarymelon as far as I know only by creating a manual Validator. It's the same as putting mixed stuff into $_GET or $_POST and try to get only numeric keys out for validation. If you can give a working example, I might be able to give a better answer

Please or to participate in this conversation.