Laravel 5.2+ 日期前后日期验证不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36514379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:34:28  来源:igfitidea点击:

Laravel 5.2+ date before and after date validation not working

phpdatelaravellaravel-5strtotime

提问by Matthew Malone

This works

这有效

'expected_at' => 'date|after:"2016-04-09 10:48:11"',

And this works

这有效

$rules['expected_at'] = 'date|after:'.$opportunity->created_at;

This does not work

这不起作用

'expected_at' => 'date|after:created_at',

The "created_at" value in the database is exactly the following

数据库中的“created_at”值正好如下

2016-04-09 10:48:11

Note the form is passing the expected_at date to the validator in the following format

请注意,表单将按以下格式将 expected_at 日期传递给验证器

2016-04-09

I assume that this means you cannot directly reference a model field in a validator?

我认为这意味着您不能直接引用验证器中的模型字段?

回答by Gordon Richard Peach

In the laravel docs for 5.3 (the version I tested):

在 5.3 的 laravel 文档中(我测试的版本):

Instead of passing a date string to be evaluated by strtotime, you may specify another field to compare against the date:

'finish_date' => 'required|date|after:start_date'

您可以指定另一个字段来与日期进行比较,而不是传递要由 strtotime 评估的日期字符串:

'finish_date' => 'required|date|after:start_date'

https://laravel.com/docs/5.3/validation#rule-after

https://laravel.com/docs/5.3/validation#rule-after

The string following the "after" rule (start_date in this case) is not a database field, but part of the input string passed to your validator.

“after”规则后面的字符串(在本例中为 start_date)不是数据库字段,而是传递给验证器的输入字符串的一部分。

So, following what you were attempting to do above, the validator was expecting a field named "created_at" to be passed along with the rest of your submitted input, which likely didn't exist since your intent was to have it read from your model, which would cause it to fail.

因此,按照您在上面尝试执行的操作,验证器期望一个名为“created_at”的字段与您提交的其余输入一起传递,该字段可能不存在,因为您的意图是让它从您的模型中读取,这会导致它失败。

Long story short, your last statement was correct. The "before/after" rules do not reference model fields, but rather other input fields passed to the validator, OR date strings that can be interpreted by "strtotime"

长话短说,你的最后一句话是正确的。“before/after”规则不引用模型字段,而是引用传递给验证器的其他输入字段,或者可以由“strtotime”解释的日期字符串

回答by tnchalise

This can be validated as:

这可以验证为:

'expected_at'  => 'required|before:' . date('Y-m-d') . '|date_format:Y-m-d',

You can even pass validator a date argument and compare at the time of validation. I assume, you are searching for date_format validator.

您甚至可以向验证器传递一个日期参数并在验证时进行比较。我假设您正在搜索 date_format 验证器。

回答by mwafi

Agree with @Gordon, and you can try this, without format in FormRequest:

同意@Gordon,你可以试试这个,在 FormRequest 中没有格式:

'expected_at' => "date|after:{$this->user()->created_at}",

Note: The reason of non-ability to refer to DB value is because no defined model to use in this stage.

注意:不能引用 DB 值的原因是因为在此阶段没有定义模型可以使用。