Reputation: 11
I am saving in the database
a DateTime
format for creating an appointment. I have two inputs one for start_time
and the other for end_time and it saves like this 2019-09-16 08:00:00
. In the view for create I have a datepicker
for selecting just the date then I have starting_hour and starting_minute and finally I have finish_hour and finish_minute
and for joining it for the correct format of DateTime
I did this:
$appointment->start_time = "".$request->date_request." ".$request->starting_hour .":".$request->starting_minute.":";
$appointment->finish_time = "".$request->date_request." ".$request->finish_hour .":".$request->finish_minute.":";
So it saves like this start_time: 2019-09-16 08:00:00
and finish_time: 2019-09-16 08:00:00
. The problem I have is on the validation because it is for appointments so finish_hour can not be before start_hour. The date won´t change because it is for appointments for one hour so i will use the same day but when trying to validate the hour I have problems. I have tried with:
'starting_hour' => 'required',
'starting_minute' => 'required',
'finish_hour' => 'required|after:starting_hour',
'finish_minute' => 'required',
But when trying to save I get:
The finish hour must be a date after starting hour.
I am guessing this is because Laravel
validation thinks also the date must be after the starting date but I only want the hour to be after the starting_hour
. So how can I validate just the finish_hour to be after the starting_hour
?
Upvotes: 1
Views: 1540
Reputation: 5149
Your hours are a number (or string representation of a number), while after:field
only works with dates.
If your hours are in military time (0-23), you should be able to use the gt:field
(greater than) rule.
'finish_hour' => 'required|gt:starting_hour',
If not and you use (1-12), it will require more complex validation. For example, an appointment from 12 pm to 1 pm would fail because 1 is less than 12.
Upvotes: 0
Reputation: 782
you can use gt or lt in the validation. For example
$request['finish_hour'] = 5;
$request['starting_hour'] = 6;
$request->validate([
'finish_hour' => 'gt:starting_hour',
]);
it will give you the validation error.
And this will pass the validation.
$request['finish_hour'] = 5;
$request['starting_hour'] = 4;
$request->validate([
'finish_hour' => 'gt:starting_hour',
]);
Upvotes: 2