Is it possible to prevent user to put end time earlier than start time with TimePickers ?

Hello ! I'm trying to make a form to show working hours but I can't find a way to prevent the user to set a endTime earlier than the startTime. Any ideas? I tried with reactive() and $get but the behaviour wasn't working as intended, could be my bad tho.
2 Replies
LeandroFerreira
maybe a custom rule:
TimePicker::make('start_time'),
TimePicker::make('end_time')
->rules([
function ($get) {
return function (string $attribute, $value, Closure $fail) use ($get) {

$startTime = Carbon::createFromFormat('H:i:s', $get('start_time'));
$endTime = Carbon::createFromFormat('H:i:s', $value);
$timeDiff = $startTime->diffInSeconds($endTime, false);

if ($timeDiff < 0) {
$fail("The time is invalid.");
}
};
},
])
TimePicker::make('start_time'),
TimePicker::make('end_time')
->rules([
function ($get) {
return function (string $attribute, $value, Closure $fail) use ($get) {

$startTime = Carbon::createFromFormat('H:i:s', $get('start_time'));
$endTime = Carbon::createFromFormat('H:i:s', $value);
$timeDiff = $startTime->diffInSeconds($endTime, false);

if ($timeDiff < 0) {
$fail("The time is invalid.");
}
};
},
])
Spârky
SpârkyOP2y ago
Thanks a lot! I managed to make my things work thanks to your code 🙂 Marking this as resolved
Want results from more Discord servers?
Add your server