I have a request that looks like the following
"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
]
knowing that each shift has its own starting and leaving time, also each shift has the option to be across midnight. for this case, we have the shift like this
#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
for example, here (in this case) the valid values can be 17:00, 18:00, 22:00, 00:30 While 14::00 is not a valid value
this is my validation rules for now that works well if the shift doesn't cross midnight
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;
}
here is what I get if the shift crosses midnight
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.