英文:
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:43
、10:58
、17: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
.
<input type="time" name="time_event" required="" min="09:00" max="18:00">
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:
'time_event' => 'required|date_format:H:i|between:09,18',
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->all(), [
'time_event' => ['required', 'date_format:H:i', 'after:08:59', 'before:18:01'],
]);
}
Try this
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论