简体   繁体   English

Laravel5.1:验证时间字段(开始时间应大于结束时间)

[英]Laravel5.1: validate time field (start time should be greater than end time)

I am new to Laravel. 我是Laravel的新手。

I am trying to validate $start_time is greater than $end_time . 我正在尝试验证$start_time大于$end_time They both are in a H:i format and are coming from database. 它们都采用H:i格式,来自数据库。

I had checked with the Laravel documentation and I got date_format:H:i for format. 我检查了Laravel文档,并获得了date_format:H:i的格式。

Use a custom validator to validate it, to do so you need to define the new custome validator under the boot() function in the AppServiceProvider class. 使用自定义验证器进行验证,为此,您需要在AppServiceProvider类的boot()函数下定义新的自定义验证器。 Check the documentation for the full description here : 此处查看文档的完整描述:

public function boot()
{
    Validator::extend('date_is_greater', function($attribute, $value, $parameters, $validator) {
        return \Carbon\Carbon::createFromFormat($parameters[0],$value)->gt(\Carbon\Carbon::createFromFormat($parameters[0],$parameters[1]));
    });
}

In this case the format is passed as the first parameter "H:i" or whatever valid DateTime format, and the $start_time is the second parameter. 在这种情况下,格式将作为第一个参数“ H:i”或任何有效的DateTime格式传递,而$ start_time是第二个参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM