php laravel 5 中的日期验证

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32541163/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:02:57  来源:igfitidea点击:

Date validation in laravel 5

phplaravel-5

提问by Texas

I have a StartDate field and a EndDate field. Both may start today or in the future. It is possible to have them both start on the same day, including today. I have to make a validation for these. What I've done so far is this:

我有一个 StartDate 字段和一个 EndDate 字段。两者都可能在今天或将来开始。可以让它们都在同一天开始,包括今天。我必须对这些进行验证。到目前为止我所做的是:

'StartDate'=>'required|date_format:Y/m/d|after:yesterday',
'EndDate'  => 'date_format:Y/m/d|after:yesterday',

What I don't know how to do is to validate true if both dates are equal as a second condition.

我不知道如何做的是验证两个日期是否相等作为第二个条件。

Anyone can help me, please ?

任何人都可以帮助我,好吗?

回答by arzhed

EDITThis was a solution up to Laravel 5.3. With Laravel 5.4, a rule after_or_equalwas introduced

编辑这是 Laravel 5.3 的解决方案。在 Laravel 5.4 中,after_or_equal引入了一条规则

https://laravel.com/docs/5.4/validation#rule-after-or-equal

https://laravel.com/docs/5.4/validation#rule-after-or-equal



I had the same need for my web app. I solved it by creating a custom Requestwith the following rulesmethod :

我对我的网络应用程序有同样的需求。我通过Request使用以下rules方法创建自定义来解决它:

public function rules()
{
    $rules = [
       'start_date'  => 'date_format:Y-m-d|after:today'
    ];

    if ($this->request->has('start_date') && $this->request->get('start_date') != $this->request->get('end_date')) {
       $rules['end_date'] = 'date_format:Y-m-d|after:start_date';
   } else {
       $rules['end_date'] = 'date_format:Y-m-d|after:today';
   }

   return $rules;
}

回答by Aqeel

This is how you can validate date:

这是您可以验证日期的方法:

'start_date'  =>  'required|date',
'end_date'    =>  'required|date|after_or_equal:start_date'

回答by Safoor Safdar

As per my understand I try to prepare custom validation rule which could help you to meet your requirement.

据我了解,我尝试准备自定义验证规则,这可以帮助您满足您的要求。

Current below mentioned rule only check if a start date is before end date.

当前下面提到的规则仅检查开始日期是否在结束日期之前。

<?php 

    namespace App\Providers;

    use Validator;
    use Illuminate\Support\ServiceProvider;

    class AppServiceProvider extends ServiceProvider{

        public function boot(){
            Validator::extend('date_after', function($attribute, $value, $parameters) {
                return strtotime( $value ) > strtotime( $this->attributes[ $parameters[0] ] );
            });
            Validator::extend('date_equal', function($attribute, $value, $parameters) {
                return strtotime( $value ) == strtotime( $this->attributes[ $parameters[0] ] );
            });
        }
    }

?>

Usage:

用法:

<?php 


$inputs = array(
  'start_date' => '2013-01-20',
  'end_date'   => '2013-01-15'
);

$rules = array(
  'start_date' => 'required',
  'end_date'   => 'required|date_after:start_date'
);

$messages = array(
  'date_after' => ":attribute must be date after Start Date."
);

$validation = Validator::make($inputs, $rules, $messages);

if( $validation->fails() )
{

  echo '<pre>';
  print_r($validation->errors);
  echo '</pre>';

}

?>

However I did not test that code with my env... but I believe its help you much to meet requirement.

但是我没有用我的环境测试该代码......但我相信它对您满足要求有很大帮助。

Please let me know your further requirement so I can provide specifically solution to your requirement.

请让我知道您的进一步要求,以便我可以针对您的要求提供具体的解决方案。