duancai1904
duancai1904
2018-11-19 16:51
浏览 256
已采纳

Laravel时间验证(小时:秒)

Trying to validate an valid 24 hour time.

Request dump,

#parameters: array:6 [▼
  "_token" => "mP2b6fmvlQG6SMgMwz9VI9kZEXtjXqp7hXUjdWdZ"
  "email" => null
  "ageLimit" => null
  "timeFrom" => "0:00"
  "timeTo" => "0:00"
  "alowed_days" => array:7 [▶]
]

Controller validation

    $validation = $request = $request->validate([
        'email' => 'email|required',
        'ageLimit' => 'required|numeric',
        'timeFrom' => 'date_format:H:i',
        'timeTo' => 'date_format:H:i',
    )];

This is failing for some reason? I'm getting "The time from does not match the format H:i."

What am I not understanding?

图片转代码服务由CSDN问答提供 功能建议

尝试验证有效的24小时时间。

请求转储,< / p>

  #parameters:array:6 [▼
“_token”=&gt;  “mP2b6fmvlQG6SMgMwz9VI9kZEXtjXqp7hXUjdWdZ”
“email”=&gt;  null 
“ageLimit”=&gt;  null 
“timeFrom”=&gt;  “0:00”
“timeTo”=&gt;  “0:00”
“alowed_days”=&gt; 数组:7 [▶] 
] 
   
 
 

控制器验证

  $ validation = $ request = $ request  - &gt;验证([
'电子邮件'=&gt;'电子邮件|必需',
'ageLimit'=&gt;'必需|数字',
'timeFrom'=&gt;'date_format:H:i',  
'timeTo'=&gt;'date_format:H:i',
)]; 
   
 
 

由于某种原因这是失败的? 我得到的“时间与格式H:i不符。”

我不理解的是什么?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanbixia7738
    duanbixia7738 2018-11-19 16:55
    已采纳

    From here: http://php.net/manual/en/function.date.php

    G 24-hour format of an hour without leading zeros 0 through 23
    ...
    H 24-hour format of an hour with leading zeros 00 through 23

    The hour values in 0:00 don't have leading zeroes, so H isn't going to match. date_format:G:i should work.

    点赞 评论
  • douhan9467
    douhan9467 2018-11-19 16:55

    From the docs:

    H 24-hour format of an hour with leading zeros

    i Minutes with leading zeros

    G 24-hour format of an hour without leading zeros

    Meaning that you should change "timeFrom" => "0:00" to "timeFrom" => "00:00" or use G instead of H.

    点赞 评论

相关推荐