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

Pricey240's avatar

Validating a date after: with a required if field

Hello, I have a question about validation...I have a form that accepts a start date, an end date, and an option to 'use dates'. I have set up a form request and in the rules i have

'use_dates'  => 'required|string|max:3',
'start_date' => 'required_if:use_dates,Yes|date_format:d/m/Y',
'end_date' => 'required_if:use_dates,Yes|date_format:d/m/Y|after:start_date',

On submit, the form always says the end date must be after the start date regardless of the option picked for use_dates. I would like it so that if use_dates is set to Yes then the start_date and end_date are both required, and that the end_date is after the start_date. If the use_dates option is anything else (the only other option is No ) then the start_date and end_date can be ignored.

Any help, or pointers in the right direction, would be greatly appreciated.

0 likes
4 replies
Pricey240's avatar

Thank you for the reply tomo_pongrac, I did have a look at creating a custom validation, but I was just wondering if it was possible to achieve this without, and just using the laravel validation. Do you mean to say that it's not possible to do it that way, and that I will have to create a custom validation?

Thanks agian for taking the time to answer my original question.

gcwilliams's avatar

Have you tried removing the "after" validation just to see if the "required_if" validation is working properly?

Pricey240's avatar

@gcwilliams, thank you for your responce. I removed the 'after' validation and the 'required if' validation works correctly, I then added back in the 'after' and the same thing was happening. However, I did then remove the default dates from the start date and end date fields on my form, and by having a blank fields the validation works as expected. It seems that if a value is passed the validation ignores the required if rule.

As there is an option in my form to say use dates or not, I think from a users point of view it's acceptable to have blank date fields, after all if the use date option is set to yes and no date is supplied, the validaiton will alert the user. ( I was trying to be clever by having the start date default to today's date)

Your answer promted me to try something I hadn't thought of....sometimes one can over complicate things!

Thank you again for taking the time to answer/help me with my question.

Please or to participate in this conversation.