我有一个如下请求
"date" => "2022-05-25"
"shift" => "2"
"attendance" => array:1 [▼
4 => array:5 [▼
"present" => "on"
"employee_id" => "4"
"time_in" => "15:00"
"time_out" => "01:00"
"note" => null
]
知道每个班次都有自己的开始和离开时间,每个班次也可以 Select 跨越午夜.对于这种情况,我们有这样的转变
#attributes: array:6 [▼
"id" => 2
"title" => "Evening Shift"
"starting_time" => "15:00"
"leaving_time" => "01:00"
"across_midnight" => 1
"user_id" => 1
]
what I need is to validate the time_in and time_out to be between starting_time and leaving_time
例如,这里(在本例中)的有效值可以是17:00、18:00、22:00、00:30,而14::00不是有效值
这是我现在的验证规则,如果轮班时间不超过午夜,它会很好地工作
public function rules()
{
$shift = Shift::find($this->shift);
$rules = [
'date' => 'required|date|date_format:Y-m-d',
'shift' => 'required|exists:shifts,id,user_id,' . auth()->id(),
'attendance' => 'required|array',
'attendance.*.employee_id' => 'required|exists:employees,id,user_id,' . auth()->id(),
];
foreach ($this->attendance as $key => $Value) {
$rules['attendance.' . $key . '.time_in'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', 'after_or_equal:' . $shift?->starting_time, 'before_or_equal:' . $shift?->leaving_time];
$rules['attendance.' . $key . '.time_out'] = [Rule::requiredIf($this->has('attendance.' . $key . '.present')), 'date_format:H:i', 'nullable', 'after_or_equal:' . $shift?->starting_time, 'before_or_equal:' . $shift?->leaving_time];
}
return $rules;
}
如果轮班时间过了午夜,我会得到这样的结果
The Time In must be a date before or equal to 01:00.
The Time Out must be a date after or equal to 15:00.