Laravel验证日期与格式不符(Laravel Validation Date does not match the format)

编程入门 行业动态 更新时间:2024-10-25 04:16:35
Laravel验证日期与格式不符(Laravel Validation Date does not match the format)

我想在Laravel中验证一个日期。 我在前端使用datepicker,JavaScript以日期格式dd-MMMM-yy将日期发送到服务器Laravel:

$scope.formData.dob = $filter('date')(scope.formData.dob, 'dd-MMMM-yy');

我的laravel有这样的验证:

$validator = Validator::make($postData, [ 'dob' => 'date_format:"dd-MMMM-yy"', ]);

但是验证器一直说日期与格式dd-MMMM-yy不匹配:

dob与格式dd-MMMM-yy不匹配。

我想也许它与格式类型有关,所以我尝试将它改为不同的格式,如d / m / y,dd-mm-yyyy等......在JavaScript日期和Laravel日期验证中但是一切都在说同样的错误日期不匹配。 输出似乎很好,它是我在验证中提到的格式。 我不明白为什么Laravel验证失败了。

我在这里错过了什么吗?

I am trying to validate a date in Laravel. I am using datepicker in the frontend where the JavaScript sends the date to the server Laravel in this format dd-MMMM-yy:

$scope.formData.dob = $filter('date')(scope.formData.dob, 'dd-MMMM-yy');

My laravel has a validation like this:

$validator = Validator::make($postData, [ 'dob' => 'date_format:"dd-MMMM-yy"', ]);

However the validator keeps saying that the date does not match the format dd-MMMM-yy:

The dob does not match the format dd-MMMM-yy.

I thought maybe it has to do with the format type so I tried changing it to different formats like d/m/y, dd-mm-yyyy, etc.. in both JavaScript date and Laravel date validation but everything keeps saying the same error that the date doesn't match. The output seems fine though and it is in the format I have mentioned in validation. I dont understand why Laravel validation fails.

Am I missing something here?

最满意答案

你使用的是错误的格式。 您需要使用标准的PHP格式,这就是Carbon以及Laravel验证所使用的格式。 你可以在PHP Docs中找到它们的date()

我相信,你真正想要的是dFy 。

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

What you actually want is d-F-y, I believe.

更多推荐

本文发布于:2023-04-27 20:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329067.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:日期   格式   Laravel   Validation   format

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!