Home > other >  Laravel validator, is there a way to validate if a given is not equal to a set date
Laravel validator, is there a way to validate if a given is not equal to a set date

Time:02-10

I have a table full of coupons, I want that multiple coupons can be active as long as their start and end date are not equal.

In Laravel I've been able to find the after:xxx, before:xxx, after_or_equal:xxx, and before_or_equal:xxx validations. However there are no validations (I've been able to find) that allow me to see if two dates are not equal.

What I am trying to validate:

  • start_date: 12-07-2021 11:00
  • end_date: 12-05-2022 11:00

What I am validating against:

  • start_date: 12-06-2021 11:00
  • end_date: 12-06-2022 11:00

I have tried using this piece of code:

'date|required|before:' . $latestCoupon->start_date. '|after:' . $latestCoupon->start_date;
'date|required|before:' . $latestCoupon->end_date. '|after:' . $latestCoupon->end_date;

Which can be read as:

'date|required|before:"12-06-2021 11:00"|after:"12-06-2021 11:00"';
'date|required|before:"12-06-2022 11:00"|after:"12-06-2022 11:00"';

However this will never work because a date can't be validated whether it is before AND after 'x' date.

So to be fair I'm stumped, in Laravel is there a way to validate if a given is not equal to a set date but before or after that date?

CodePudding user response:

  •  Tags:  
  • Related