本文介绍了Laravel5.1:验证时间字段(开始时间应大于结束时间)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Laravel的新手.

I am new to Laravel.

我正在尝试验证$start_time是否大于$end_time.它们都是H:i格式,并且来自数据库.

I am trying to validate $start_time is greater than $end_time. They both are in a H:i format and are coming from database.

我已经查看了Laravel文档,并获得了date_format:H:i的格式.

I had checked with the Laravel documentation and I got date_format:H:i for format.

推荐答案

使用自定义验证器对其进行验证,为此,您需要在AppServiceProvider类中的boot()函数下定义新的自定义验证器.检查文档以获取完整说明此处:

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. 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]));
    });
}

在这种情况下,格式将作为第一个参数"H:i"或任何有效的DateTime格式传递,而$ start_time是第二个参数.

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.

这篇关于Laravel5.1:验证时间字段(开始时间应大于结束时间)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 00:47