Laravel 5.1 Date_Format验证器在Y-m-d格式上失败。

时间:2022-06-24 21:29:30

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:

我开始使用请求对象来验证传入的post数据,并且我已经看到了其他的例子,说明了其他人是如何使用date_format的,但是我似乎不能通过使用PHP的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.

更新:它们是date_parse_from_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:

它抛出一个NotFoundHttpException,但如果我在validator请求中注释start_at和end_at,它会通过适当的有效负载进入我的控制器操作:

{
    "rental_id": "1",
    "start_at": "2015-02-30",
    "end_at": "2015-02-30"
}

1 个解决方案

#1


0  

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!!!

显然,date_format验证失败了,因为我随机选择了2015-02-30来测试我的API,但那一天并不存在,因为那将是2月30日……哦,耻辱和浪费的时间。感谢@ceejayoz提供的所有帮助!!!

#1


0  

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!!!

显然,date_format验证失败了,因为我随机选择了2015-02-30来测试我的API,但那一天并不存在,因为那将是2月30日……哦,耻辱和浪费的时间。感谢@ceejayoz提供的所有帮助!!!