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

ondrejcz's avatar

Conditional inclusion of fields to validate

On a post request, I am validating several form fields. Most of them suffice with a simple 'required' validation logic. However, I have one field, which is either required, or may be empty in case a checkbox is checked.

This is it:

public function store(Request $request) {
        $formFields = $request -> validate([
            'firstName' => 'required',
            'surname' => 'required'
        ]);
}

I need to add a required validation for vat unless the iHaveNoVatId checkbox is checked. Is this possible?

0 likes
1 reply
sosaheri's avatar
sosaheri
Best Answer
Level 4

Hi you can conditional validate in this way

'vat' => 'required_if:iHaveNoVatId,1',

Please or to participate in this conversation.