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

Mushr00m's avatar

Date format validation and 0000-00-00 00:00:00

Hi !

I'm on Laravel 4.2 and want to validate a date from a text field. My issue is that I use the rule date_format:Y-m-d H:i:s for validating the date format. But I also want to accept 0000-00-00 00:00:00 when the user enter nothing. Of course this is not a valid date so I don't know how to handle that... Any ideas ?

Thanks !

0 likes
10 replies
bashy's avatar

What about validating it but not requiring it? Regex maybe?

Mushr00m's avatar

Yes I validate it, but not require it. Regex can do the trick but I loose the date validation. There is no way to add like an except rule for validation ?

bashy's avatar

What about this?

protected $rules = [
    'some_date' => 'required|date_format:d/m/Y|regex:/[0-9]{2}\/[0-9]{2}\/[0-9]{4}/',
 ];
Mushr00m's avatar

But doing "date_format:d/m/Y" will and up blocking the date as well because when sending 0000-00-00 00:00:00 or 00/00/0000 in your case it's not a valid date, it should be minimum of 01/01/0001...

bashy's avatar

Why don't you add validation for the date but not require it, then if the date POST is empty, put the default into the table (0000-00-00 00:00:00)

Mushr00m's avatar

That's what I do right now. But I was wanting to display 0000-00-00 00:00:00 when nothing is enter so the user understand. But doing that the post is not empty so the validation is done on this value and failes.

Mushr00m's avatar

There are differents dates. One for example is for a filter for date range, so I have From and To and need a valid date format but also accept 0000-00-00 when the user chosse nothing.

bashy's avatar
bashy
Best Answer
Level 65

Not sure why they'd need to see their selection if they don't add one anyway but I don't know what the page is like so forgive me on that :P

Probably need to write a custom validation rule for that anyway.

Mushr00m's avatar

Yes I will have a look at custom validation rule, it may be the way to go ! Thanks

Please or to participate in this conversation.