skip to Main Content

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-dTH:i:s.uZ'

and this:

date_format:Y-m-d

but I get 422 every time.

3

Answers


  1. Simply adding this will resolve your issue,

    Solution 1:

    'required|date',
    

    But if you need some exact solution you can do something like this,

    Solution 2:

    'required|date_format:Y-m-dTH:i:s.vZ',
    

    But the recommended solution is the first one, since it handles most of the date formats. Hope this resolves your issue.

    Login or Signup to reply.
  2. u from the date format represents microseconds (6 digits). Therefor you need to use v which represents only milliseconds (3 digits).

    Therfor the solution would be:

    'required|date_format:Y-m-dTH:i:s.vZ'
    

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

    Login or Signup to reply.
  3. 'date' => [
        'required',
        'regex:/^d{4}-d{2}-d{2}Td{2}:d{2}:d{2}.d{3}Z$/'
    ]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search