如何在 laravel 中验证时间
How to validate time in laravel
我想验证 Laravel 中的时间。例如:- 我希望当用户输入晚上 8 点到晚上 10 点之间的时间时,它会显示验证错误。我怎样才能在 Laravel
中实现这一目标
您可能应该看一下 this 部分文档。自定义规则听起来很适合您。
试试这个代码
use Validator;
use Carbon\Carbon;
$timeHours = "7:00 PM";//change it to 8:00 PM,9:00 PM,10:00 PM it works
$time = Carbon::parse($timeHours)->format('H');
$request['time'] = $time;
$validator = Validator::make($request->all(), [
'time' => ['required','integer','between:20,22']
]);
if ($validator->fails()) {
dd($validator->errors());
}
此代码可能适用于您的控制器。但是,它不会验证不同日期的时间(例如晚上 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
}
使用date_format规则验证
date_format:H:i
来自文档
date_format:format
验证中的字段必须匹配根据 date_parse_from_format PHP 函数定义的格式。
在 Laravel 5.6 中:
位于 /app/Http/Requests/YourRequestValidation
的文件内
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class YourRequestValidation extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'initial_time' => 'required',
'end_time' => 'required',
'end_time' => 'after:initial_time'
];
}
/**
* Custom message for validation
*
* @return array
*/
public function messages()
{
return [
'initial_time.required' => 'Please, fill in the initial time',
'end_time.required' => 'Please, fill in the end time.',
'end_time.after' => 'The end time must be greater than initial time.'
];
}
}
$beginHour = Carbon::parse($request['hour_begin']);
$endHour = Carbon::parse($request['hour_end']);
if($beginHour->addMinute()->gt($endHour)){
return response()->json([
'message' => 'end hour should be after than begin hour',
], 400);
}
创建 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'
];
}
}
我想验证 Laravel 中的时间。例如:- 我希望当用户输入晚上 8 点到晚上 10 点之间的时间时,它会显示验证错误。我怎样才能在 Laravel
中实现这一目标您可能应该看一下 this 部分文档。自定义规则听起来很适合您。
试试这个代码
use Validator;
use Carbon\Carbon;
$timeHours = "7:00 PM";//change it to 8:00 PM,9:00 PM,10:00 PM it works
$time = Carbon::parse($timeHours)->format('H');
$request['time'] = $time;
$validator = Validator::make($request->all(), [
'time' => ['required','integer','between:20,22']
]);
if ($validator->fails()) {
dd($validator->errors());
}
此代码可能适用于您的控制器。但是,它不会验证不同日期的时间(例如晚上 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
}
使用date_format规则验证
date_format:H:i
来自文档
date_format:format
验证中的字段必须匹配根据 date_parse_from_format PHP 函数定义的格式。
在 Laravel 5.6 中:
位于 /app/Http/Requests/YourRequestValidation
的文件内namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class YourRequestValidation extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'initial_time' => 'required',
'end_time' => 'required',
'end_time' => 'after:initial_time'
];
}
/**
* Custom message for validation
*
* @return array
*/
public function messages()
{
return [
'initial_time.required' => 'Please, fill in the initial time',
'end_time.required' => 'Please, fill in the end time.',
'end_time.after' => 'The end time must be greater than initial time.'
];
}
}
$beginHour = Carbon::parse($request['hour_begin']);
$endHour = Carbon::parse($request['hour_end']);
if($beginHour->addMinute()->gt($endHour)){
return response()->json([
'message' => 'end hour should be after than begin hour',
], 400);
}
创建 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'
];
}
}