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

Tappad's avatar

Laravel Validator Rules in Array

The docs of the regex validation says:

"Note: When using the regex pattern, it may be necessary to specify rules in an array instead of using pipe delimiters, especially if the regular expression contains a pipe character"

https://laravel.com/docs/5.5/validation#rule-regex

But it doesn't say how to specify the rules as an array. How do I pass rules to the validator as an array?

What I'm looking for is something like:

$min = 8;
    $validator = Validator::make($request->all(), [
        'password' => array(
            'required' => true,
            'min' => $min
        )
    ]);

But that isn't working.

0 likes
6 replies
janderson's avatar
$request->validate([
    $request->password => 'required|min:8'
]);

Maybe I'm missing something from your example, but you should try that. There is no regex in your example.

Tappad's avatar

Thanks for your reply!

There's no regex in my example. I just want to know how to pass the rules as an array.

$min = 8;
$request->validate([
    $request->password => 'required|min:'.$min
]);

Isn't clean enough for me.

janderson's avatar

Your temporary variable $min = 8 is not useful, and needs to be a string.

The "clean" code you posted will not run, I'm not sure how to help you, but I'll indulge.

$min = '8';
$request->validate([
    $request->password => ['required' => true, 'min' => $min]
]);

Maybe that's what you are after. Cheers!

sutherland's avatar

Just use a double quote:

$validator = Validator::make($request->all(), [
    'password' => [
        'required',
        "min:$min"
    ]
]);
cschorn's avatar

What is meant is that it is not necessary to pass the validation rule and parameters as a concatenated string. You can use an array and pass the variables pre-split, like so:

$min = 8;

$validator = Validator::make($request->all(), [
    'password' => [
        'required',
        ['min', $min],
    ],
]);

If you want to validate the regex a|b you have the problem that the rule parser does not understand required|regex:/a|b/ correctly, so you have to use the array form:

$rules = [
    'some_field' => [
        ['regex', '/a|b/']
    ]
];

Also, the array syntax is safer if your interpolated string might contain a pipe character.

1 like
krabeats's avatar

I know this is an old post but it can't hurt to add something helpfull right?

The Laravel docs mean you should do the following (for multiple regex):

'postcode' => [ 'required', 'max:8', ['regex', '/^[a-zA-Z]{1,2}[0-9]{1,2} [0-9]{1}[a-zA-Z]{2}|^[a-zA-Z]{1,2}[0-9]{2,3} [a-zA-Z]{2}/'], ]

As shown above, the '|' is now applicable, and, you now have two regex that will be used in an either/or fashion.

Honestly I must credit @cschorn as there post is where I learned this. :)

Again, I know this is an old post but it can't hurt to add something helpfull right?

Please or to participate in this conversation.