0

I'm starting to use Request objects to validate incoming post data, and I've seen examples of how others are using date_format, but I can't seem to get dates to pass even though I'm using a format that passes when you use PHP's date_parse_from_format:

print_r(date_parse_from_format('Y-m-d','2015-07-27'));

Output

UPDATE: their is a warning in date_parse_from_format, but I don't understand why as it reflects the format.

{
    "year": 2015,
    "month": 2,
    "day": 30,
    "hour": false,
    "minute": false,
    "second": false,
    "fraction": false,
    "warning_count": 1,
    "warnings": {
      "10": "The parsed date was invalid"
    },
    "error_count": 0,
    "errors": [],
    "is_localtime": false
}

Validator

public function rules()
{
    return [
        'rental_id' => 'required|exists:rentals,id',
        'start_at' => 'required|date_format:Y-m-d',
        'end_at' => 'required|date_format:Y-m-d'
    ];
}

Using PostMan I'm sending in a payload of:

rental_id = 1 as text
start_at = 2015-02-30 as text
end_at = 2015-02-30 as text

And it throws a NotFoundHttpException, but if I comment out start_at and end_at in the validator request it passes, and enters my controllers action with the proper payload:

{
    "rental_id": "1",
    "start_at": "2015-02-30",
    "end_at": "2015-02-30"
}
mtpultz
  • 17,267
  • 22
  • 122
  • 201

1 Answers1

2

Apparently, the date_format validation failed as I randomly chose 2015-02-30 to test my API, but that day doesn't exist as that would be February 30... oh the shame and the wasted time. Thanks to @ceejayoz for all the help!!!

mtpultz
  • 17,267
  • 22
  • 122
  • 201
  • 1
    Hmm, that's odd. The underlying code should work with a future date. – ceejayoz Jul 22 '15 at 19:26
  • 1
    The code for `validateDateFormat` would do `date_parse_from_format('Y-m-d', '2015-07-30')` and `date_parse_from_format('Y-m-d', '2015-07-10')` both of which work without errors or warnings when I try them manually. – ceejayoz Jul 22 '15 at 19:28
  • Hi @ceejayoz, you're right got it I randomly chose a date just to test, and I managed to randomly choose the only month that has only 28 days. Sigh! – mtpultz Jul 22 '15 at 19:30
  • 1
    Ah ha! February 30th is what tripped you up. – ceejayoz Jul 22 '15 at 19:33