Search code examples
phplaraveldate-formatting

Laravel date_format Validation Fails on Textual Month


My JavaScript library datepicker returns dates in a format "March 2019."

Carbon can decode it.

$date = Carbon::createFromFormat('M Y', $request->month);

Laravel fails on date_format validation.

$request->validate([
    'month' => [
        'required',
        'date_format:M Y',
    ],
]);

saying

The month does not match the format M Y.

I have tried all PHP date formats from here : M, MM, mm, m.


Solution

  • You're using the wrong format. You need to use standard PHP formats, which is what Carbon and Laravel's validation uses. You can find them in the PHP Docs for date()

    So change your rule to:

    date_format:"F Y"