In my app, the user selects date from a datepicker and the date is then displayed in the input in a format that corresponds user's locale.

提交表单时,我想验证各自的日期,但是验证器不知道提交日期的日期格式.

My question is whether I should mutate the date into Y-m-d before it is passed to validator or is there a way I can tell the Validator the right format to validate in?

推荐答案

The easier option is to use the Laravel date_format:format rule (https://laravel.com/docs/5.5/validation#rule-date-format). It's a built-in function in Laravel without the need for a custom rule (available in Laravel 5.0+).

你可以做:

$rule['date'] = 'required|date_format:d/m/Y';

or

$rule['date'] = 'required|date_format:Y-m-d';

Laravel相关问答推荐

Inertia React中的错误文件上传更新

(1/1) ErrorException ReflectionFunction::__construct() ex

如何在生产中的 laravel 应用程序中获取标头请求值?

如何通过单击进入 vs 代码中的类

Laravel 5 将错误发送到Electron邮件

在laravel中将数据插入数据透视表

如何使用 Eloquent Laravel 更新集合

Laravel 使用 Storage::put 生成唯一 ID

如何在 Laravel 中实现自己的 Faker 提供程序

Eloquent ORM,deleted_at 使用软删除时没有索引

当表中不存在列时如何将 paginate() 与 having() 子句一起使用

WhereNotExists Laravel Eloquent

Laravel 5:在同一字符串上使用 bcrypt 给出不同的值

Laravel:验证 json 对象

Laravel 存储链接不适用于生产

如何在 Laravel 视图中找到当前语言?

Elastic search全文与mysql全文?

如何在 laravel 用户删除中重置自动增量?

如何在 Laravel 5.2 中使用 OR 条件将多个参数传递给中间件

在 Laravel 5 中找不到类App\Http\Controllers\admin\Auth