lundi 16 septembre 2019

How to validate DateTime Laravel

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 valiation because it is for appointments so finish_hour can not be before start_hour. The date won´t change because it is for appoinments 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?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire