Laravel: How to validate that a variable is an array OR a string matching a regex Laravel: How to validate that a variable is an array OR a string matching a regex laravel laravel

Laravel: How to validate that a variable is an array OR a string matching a regex


The most "elegant" workaround I can think of is creating a Custom Validation Rule, but you could also try conditionally adding the array and the regex rules.

But how about the obvious, checking the type of the variable in order to add the appropriate rule?

//dummy data, could be from $request->all()$data = ['keys' => '12345'];$rules = [    'keys' => is_array($data['keys']) ? 'array' : 'regex:\d+'];$validation = Validator::make($data, $rules);

Quick and simple, but it's a waste of an array check and will always give the regex error message.