Hi, i have a API call which i recieve 2 query parameters, start_date and end_date. I want to validate that the time between the two dates is less than 5 days, is there a way to do this?
You can use carbon and subtract the 2 dates within the closure and return $fail($attribute.' is greater than 5 days'); or whatever you want the error message to be if it fails.
I think that can do the trick. something like this, just that i am unsure how to add that to the existing date_end validation rules, as in the example it is done different syntax.
$validatedData = $request->validate([
'date_start' => 'required|date',
'date_end' => 'required|date|after_or_equal:date_start',
function ($attribute, $value, $fail){
$start = Carbon::parse($request->get('date_start'));
$end = Carbon::parse($request->get('date_end'));
if($end->diffInDays($start)>1){
$fail($attribute.' date range is bigger than 1 day.');
}
}
]);