1

我已经制作了名为 EventRequest 的自定义请求来验证输入我有两个日期属性,第一个是“开始”,第二个是“结束”。“结束”必须大于或等于开始。注意:laravel 5.1 没有 after_or_equal 函数

这是我的代码

public function rules()
{
    return [
      'name'          =>'required|min:3|max:255',
      'place'         => 'required|min:3|max:255',
      'start_date'    => 'required|date|after:yesterday',
      'end_date'      => 'required|date|after:yesterday',
      'start_time'    => 'required|date_format:H:i',
      'end_time'      => 'required|date_format:H:i'
    ];
}

如果我设置了一条规则,例如 start_date 必须是 before:end_date 但如果它们在同一天,则会引发错误。有什么解决方案吗?提前致谢!

4

2 回答 2

0

Validator手动使用解决方案

public function validateForm(Request $request){
    $end_date = date('Y-m-d', strtotime($request->end_date. ' -1 day' ));
    $messages = [
        "end_date.after" => "End date must be equal or greater than start date.",
    ];
    $rules = [
            'start_date'    => 'required|date|after:yesterday',
            'end_date'      => 'required|date|after:'.$end_date,
    ];
    return Validator::make($requestAll, $rules);
}
于 2018-05-19T07:40:14.243 回答
-1

开始日期晚于:昨天...所以 end_date 可以晚于 start_date ...

 public function rules()
{
    return [
      'name'          =>'required|min:3|max:255',
      'place'         => 'required|min:3|max:255',
      'start_date'    => 'required|date|after:yesterday',
      'end_date'      => 'required|date|after:start_date',
      'start_time'    => 'required|date_format:H:i',
      'end_time'      => 'required|date_format:H:i'
    ];
}
于 2018-05-19T00:32:43.723 回答