我想在Laravel中验证时间。例:-我希望当用户输入时间从晚上8点到晚上10点,然后它将显示验证错误。我如何在Laravel中实现这一点?
发布于 2017-12-21 16:57:54
使用date_format规则验证
date_format:H:i来自文档
date_format:format要验证的字段必须与根据date_parse_from_format PHP函数定义的格式匹配。
发布于 2016-09-14 05:44:18
也许这段代码可以在你的控制器中工作。然而,它不会验证不同日期的时间(例如晚上9点到第二天凌晨3点)。本例中的time_start和time_end应该作为HH:mm提供,但是您可以很容易地更改它。
public function store(Illuminate\Http\Request $request)
{
$this->validate($request, [
'time_start' => 'date_format:H:i',
'time_end' => 'date_format:H:i|after:time_start',
]);
// do other stuff
}发布于 2020-01-22 18:19:29
创建DateRequest,然后添加
<?php
namespace App\Http\Requests\Date;
use App\Http\Requests\FormRequest;
class DateRequest extends FormRequest
{
/**
* --------------------------------------------------
* Determine if the user is authorized to make this request.
* --------------------------------------------------
* @return bool
* --------------------------------------------------
*/
public function authorize(): bool
{
return true;
}
/**
* --------------------------------------------------
* Get the validation rules that apply to the request.
* --------------------------------------------------
* @return array
* --------------------------------------------------
*/
public function rules(): array
{
return [
'start_date' => 'nullable|date|date_format:H:i A',
'end_date' => 'nullable|date|after_or_equal:start_date|date_format:H:i A'
];
}
}https://stackoverflow.com/questions/39467452
复制相似问题