[英]how to make an input date field greater than or equal to another date field using validation in laravel
Hi iam developing an application using laravel is there any way to make an input date field greater than or equal to another date field using validation.嗨,我正在使用 laravel 开发应用程序,有什么方法可以使用验证使输入日期字段大于或等于另一个日期字段。
I know that i can achieve this through jquery and i have already got that working, but i want to know whether is this achievable through laravel validation since laravel has got some predefined validations.我知道我可以通过 jquery 实现这一点,而且我已经开始工作了,但我想知道这是否可以通过 laravel 验证实现,因为 laravel 有一些预定义的验证。
For Example例如
protected $validationRules = array
(
'a' => 'date',
'b' => 'date|(some validation so that the b value is greater than or equal to that of 'a')'
);
EDIT编辑
If there is any other approach to solve the problem using laravel concept please tell me如果有任何其他方法可以使用 Laravel 概念解决问题,请告诉我
I tried我试过
Validator::extend('val_date', function ($attribute,$value,$parameters) {
return preg_match("between [start date] and DateAdd("d", 1, [end date])",$value);
});
Thanks in advance提前致谢
I had the same problem.我有同样的问题。 before
and after
does not work when dates could be the same.当日期可能相同时, before
和after
不起作用。 Here is my short solution:这是我的简短解决方案:
NOTE: Laravel 5.3.25 and later have new built in rules: before_or_equal
and after_or_equal
注意: Laravel 5.3.25 及更高版本有新的内置规则: before_or_equal
和after_or_equal
// 5.1 or newer
Validator::extend('before_or_equal', function($attribute, $value, $parameters, $validator) {
return strtotime($validator->getData()[$parameters[0]]) >= strtotime($value);
});
// 5.0 & 4.2
Validator::extend('before_or_equal', function($attribute, $value, $parameters) {
return strtotime(Input::get($parameters[0])) >= strtotime($value);
});
$rules = array(
'start'=>'required|date|before_or_equal:stop',
'stop'=>'required|date',
);
Emil Aspman's answer is correct, but doesn't work for Laravel 5.2. Emil Aspman 的回答是正确的,但不适用于 Laravel 5.2。 this solution works for Laravel 5.2:此解决方案适用于 Laravel 5.2:
Validator::extend('before_equal', function($attribute, $value, $parameters, $validator) {
return strtotime($validator->getData()[$parameters[0]]) >= strtotime($value);
});
Yes, you can use after:date
or before:date
like this:是的,您可以像这样使用after:date
或before:date
:
protected $rules = array(
'date' => 'after:'.$yourDate
);
or alternatively或者
protected $rules = array(
'date' => 'before:'.$yourDate
);
It will do exactly what you described.它将完全按照您的描述执行。 Also check out the official documentation .另请查看官方文档。 You can also specify rules of your own using custom validation rules .您还可以使用 自定义验证规则指定自己的 规则。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.