如何使用laravel中的验证使输入日期字段大于或等于另一个日期字段 [英] how to make an input date field greater than or equal to another date field using validation in laravel

查看:648
本文介绍了如何使用laravel中的验证使输入日期字段大于或等于另一个日期字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我使用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.

我知道我可以通过jquery做到这一点,并且我已经可以使用它,但是我想知道通过laravel验证是否可以通过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.

例如

protected $validationRules = array
    (   
      'a' => 'date',
      'b' => 'date|(some validation so that the b value is greater than or equal to that of 'a')'
    );

编辑

如果还有其他使用laravel概念解决问题的方法,请告诉我

If there is any other approach to solve the problem using laravel concept please tell me

我尝试过

Validator::extend('val_date', function ($attribute,$value,$parameters) {
    return preg_match("between [start date] and DateAdd("d", 1, [end date])",$value);
});

预先感谢

推荐答案

我遇到了同样的问题.当日期可能相同时,beforeafter不起作用.这是我的简短解决方案:

I had the same problem. before and after does not work when dates could be the same. Here is my short solution:

注意: Laravel 5.3.25和更高版本具有新的内置规则: before_or_equal after_or_equal

NOTE: Laravel 5.3.25 and later have new built in rules: before_or_equal and 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',
);

这篇关于如何使用laravel中的验证使输入日期字段大于或等于另一个日期字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆