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

devmain's avatar

Validation regex with laravel

I need to validate the array name field only when the ID starts with the letter E. I've tried several ways, including with regex and it doesn't work. Specifically the name field is not validating, the rest is normal.

public function rules(): array
    {
        return [
            'products' => 'present|required|array|min:1',
            'products.*.id' => 'required',
            'products.*.name' => 'required_if:products.*.id|regex:/^e/',
         ];
    }
0 likes
7 replies
bestmomo's avatar

You can make a custom validation and use it in your rule:

'products.*.name' => 'required_if:products.*.id,starts_with_e',
jbahena82's avatar

Maybe try writing a conditional validations instead on relying on regex. That way you write your own condition without relying too much on regex ? Just a thought.

devmain's avatar

I solved the problem, creating a custom validation custom. Thanks for all

Please or to participate in this conversation.