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

johncarter's avatar

Conditional validation on arrays

I have a Form Request that has some validation rules conditionally met on a checkbox (main_dealer) being checked:


    public function rules()
    {
        return [
            'bid_lines.*.description' => ['required', 'min:3', 'max:100', 'string'],
            'bid_lines.*.brand' => ['required_without:bid_lines.*.main_dealer', 'in:genuine,oe,aftermarket'],
        ];
    }

The above required_without code works - the brand select input isn't required if the main_dealer checkbox is checked.

The problem is that the in: condition is still being triggered even if it's not required, so is failing. Can someone help me figure this out?

0 likes
5 replies
johncarter's avatar

What about not even POSTing the data in the first place? So when that checkbox is selected I remove it from the DOM then use sometimes? Any reason that's bad?

Nakov's avatar
Nakov
Best Answer
Level 73

@johncarter I don't think it is bad but it is extra work. Why not allowing it to be nullable instead?

What kind of data does it contain when the main_dealer checkbox is checked?

1 like
johncarter's avatar

That's got it! nullable - so simple

'bid_lines.*.brand' => ['required_without:bid_lines.*.main_dealer', 'nullable', 'in:genuine,oe,aftermarket'],

Thanks very much @nakov!

Nakov's avatar

@johncarter and why is it your answer the Best Answer now? :) I don't understand :)

johncarter's avatar

More easy to scan to the solution, but as you asked I'll change it to yours. Sorry.

Please or to participate in this conversation.