Laravel 5.3 date validator: equal to or after star

2019-04-18 17:19发布

问题:

I'm using Laravel 5.3 to validate start_date and end_date for an event. end_date should be equal to start_date or the after date. end_date >= start_date

$validator = Validator::make($data, [
    'start_date'    => 'required|date',
    'end_date'      => 'required|date|after:start_date',
]);

I tried to use after, but it only works for end_date > start_date. Of course, I can add custom rule using Validator::extend, but I'd like to know if we can do without adding custom rule.

Is there any way to add negative rule or add >= rule?

Thanks

回答1:

$validator = Validator::make($data, [
    'start_date'    => 'required|date',
    'end_date'      => 'required|date|after_or_equal:start_date',
]);

Use after_or_equal



回答2:

upgarate to 5.4 and you can use after_or_equal see https://laravel.com/docs/5.4/validation#rule-after-or-equal



回答3:

Actually, you can also use after_or_equal and before_or_equal when using at least Laravel version 5.3.31. This may help to avoid having to upgrade to a higher Laravel version.