Laravel请求验证:检查时间是否在两个时间之间?

huangapple go评论63阅读模式
英文:

Laravel Request Validation: Check if time is between two times?

问题

我在视图中有一个类型为 time 的输入,它接受最小值为 09:00,最大值为 18:00

<input type="time" name="time_event" required="" min="09:00" max="18:00">

在这种情况下,用户可以输入不同的时间,如 09:4310:5817:33,只要时间从 09:00 开始,到 18:00 结束。

考虑到这一点,我在 Laravel 中设置了一个验证规则:

'time_event' => 'required|date_format:H:i|between:09,18',

问题是 between 规则不起作用。

我如何使用 Laravel 进行验证,以确保时间类型字段符合我的系统要求的标准?

英文:

I have a input of type time inside my view, which accepts a minimum value of 09:00 and a maximum value of 18:00.

&lt;input type=&quot;time&quot; name=&quot;time_event&quot; required=&quot;&quot; min=&quot;09:00&quot; max=&quot;18:00&quot;&gt;

In this case, the user can enter several different times like 09:43, 10:58, 17:33... but as long as the time starts from 09:00 and goes until 18:00.

With that in mind I have a validation rule with Laravel:

&#39;time_event&#39; =&gt; &#39;required|date_format:H:i|between:09,18&#39;,

The problem is that the between rule is not working correctly.

How can I validate with laravel if the time type field is within the standards required by my system?

答案1

得分: 1

public function validateTime(Request $request)
{
    $validator = Validator::make($request->all(), [
        'time_event' => ['required', 'date_format:H:i', 'after:08:59', 'before:18:01'],
    ]);
}
英文:
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

public function validateTime(Request $request)
{
    $validator = Validator::make($request-&gt;all(), [
        &#39;time_event&#39; =&gt; [&#39;required&#39;, &#39;date_format:H:i&#39;, &#39;after:08:59&#39;, &#39;before:18:01&#39;],
    ]);
}

Try this

huangapple
  • 本文由 发表于 2023年6月1日 00:20:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375541.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定