Search code examples
phplaravellaravel-formrequest

How to validate date in laravel/formrequest


I have this type of date:

"Date": "2024-09-06T06:58:19.326Z",

How should I validate it in my FormRequest in Laravel?

I've tried this:

'required|date_format:Y-m-d\TH:i:s.u\Z'

and this:

date_format:Y-m-d

but I get 422 every time.


Solution

  • u from the date format represents microseconds (6 digits). To match your datetime format you need to use v which represents only milliseconds (3 digits).

    Therfor the solution would be:

    'required|date_format:Y-m-d\TH:i:s.v\Z'
    

    See: https://www.php.net/manual/de/datetime.format.php