我需要一些关于Laravel表单验证的帮助:
刀片:
<input type="datetime-local" class="form-control" name="date[]">验证:
$now = Carbon::now();
$after = Carbon::createFromDate(2025,01,01);
return request()->validate([
'name' => ['required'],
'description' => ['required'],
'date' => ['required', 'before:'. $now, 'after:'.$after]
]);我认为这应该是有效的,但是不管它总是抛出什么输入值和错误。我的猜测是日期格式不匹配,Laravel无法进行比较。我有一个不同形式的基本相同的比较,但只与日期类型,这是完美的工作。
我在这里发现了一些类似的问题,但对我来说都不起作用。我没主意了。其他人也有同样的问题吗?
提前感谢您的答复
发布于 2020-04-12 10:34:25
根据您对date字段使用的验证规则,总是抛出一个错误是非常有意义的。原因是您希望您的date在当前时间之前为,同时在2025-01-01之后为,这不能产生真实的输出。
您需要做的是将您的规则值反转为
'date' => 'required|array', // check date is set and is an array
'date.*' => ["before:$after", "after:$now"] // each element in the array needs to follow the before and after rules这样,您要求每个date值在当前白天之后为,在2025-01-01之前为。
https://stackoverflow.com/questions/61165000
复制相似问题