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

maxlovell's avatar

validate array names with regex in laravel

I am receiving form input with a POST request and am looking to validate the names in my array using regex as they follow a specific pattern. Here is a log of the received $request variable:

[2023-09-20 11:35:13] local.INFO: array ( '_token' => 'XXX'', 'type' => 'implementation', 'q1' => '0', 'q2' => '3', 'q3' => '1', 'q4' => '1', 'q5' => '2', 'q6' => '0', )

and I was hoping to validate the array with something like:

    $validated = $request->validate([
        '_token' => 'regex:/^[A-Z0-9]*$/i',
        'type' => ['bail','required','regex:/^(implementation|outcomes)$/i'],
        '​​/^q[1-9][0-7]?(_ev)?$/' => 'required|regex:/^[0-3]$/i'
    ]);

using regex for the possible names (_ev can sometimes be part of the name of a variable). Is there any way to do validate array names in laravel without hardcoding each name?

Thanks

0 likes
1 reply
Snapey's avatar

never seen anyone use regex for the rule names.

Do you have an unknown number of inputs? If not, just list them out. If so then use form array syntax for the fields

Please or to participate in this conversation.