【问题标题】:Laravel date_format rule not workingLaravel date_format 规则不起作用
【发布时间】:2017-02-15 12:56:39
【问题描述】:

我正在尝试对我的输入使用日期时间格式规则,如下所示:

H:i:s

我的模型规则如下所示:

    public static $rules = [
        ...
        'start'    => [
            'min:8',
            'date_format:"H:i:s"',
        ],
    ];

在我的控制器中,我这样称呼它:

        $validator = Validator::make($request->all(), Product::$rules);

        if($validator->fails())
        {
            return Redirect::back()->withInput()->withErrors($validator);
        }

如果我dd($validator) 我得到关注:

  #rules: array:7 [▼
     ...
    "start" => array:2 [▼
      0 => "min:8"
      1 => "date_format:"H:i:s""
    ]
  ]

我将 00:h:01 插入输入字段,但没有收到任何错误消息。

【问题讨论】:

  • 如果将 'date_format:"H:i:s"' 更改为 'date_format:H:i:s' 会怎样?所以你不要逃避格式。
  • 还是不行

标签: php laravel datetime format laravel-5.1


【解决方案1】:

删除 'min:8' 然后再次测试

 public static $rules = [
        ...
        'start'    => [
            'date_format:"H:i:s"',
        ],
    ];

仅供参考,Laravel date_format 规则在内部使用 date_parse_from_format() 进行验证

欲了解更多信息:https://laravel.com/docs/5.1/validation

我刚刚检查过,它对我有用:

print_r(date_parse_from_format("H:i:s", "00:h:01"));

【讨论】:

  • print_r(date_parse_from_format("H:i:s", "00:h:01")); 是返回真还是假?
  • 不,它不会,它会返回一个数组,如果有错误,它将在该数组内
猜你喜欢
  • 2020-03-01
  • 2018-11-09
  • 1970-01-01
  • 2022-01-13
  • 2017-06-21
  • 2016-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多