ldragicevic
ldragicevic

Reputation: 711

Laravel Eloquent date_format Validation fails on given format

I want to create validation rule to validate incoming date. Format I want to validate is Y-m-d H:i:s. Here's my body request which I am validating:

{ "date":"2015.10.5 10:30:10" }

And here's my validation rule:

'date' => 'required|date_format:"Y.m.d H:i:s"',

And it returns:

{"date":["The date does not match the format Y.m.d H:i:s."]}

Upvotes: 3

Views: 4044

Answers (1)

tptcat
tptcat

Reputation: 3961

If you want to be able to pass the day without a leading zero, then for the day part of your datetime you need to use j instead of d.

'date' => 'required|date_format:"Y.m.j H:i:s"',

That will work for the example you have above.

If you are going to always have a leading zero in your day (so, 05 instead of just 5, then the date format you already have will work.

Upvotes: 7

Related Questions