Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jericopulvera's avatar

Custom Validation Rule does not work on every 2nd attempt

Validator::extend('available', function ($attribute, $value, $parameters, $validator) {
            $property = Property::find(request('id'));

            if ( ! $property) {
                return true;
            }

            foreach ($property->blockedDates()->get() as $booking) {
                if (
                    Carbon::parse(request()->dates['from']) <= Carbon::parse($booking->departure) &&
                    Carbon::parse(request()->dates['to']) >=  Carbon::parse($booking->arrival)
                ) {
                    return false;
                }
                return true;
            }

            return true;
        });

What I'm doing is I'm checking if your requested dates intersected with the property blocked dates.

My problem is that the Custom Validation Rule does not work on every 2nd attempt.

but when I do this. It works on every request

Validator::extend('available', function ($attribute, $value, $parameters, $validator) {
            return false;
        });

0 likes
1 reply
Dry7's avatar

try

Validator::extend('available', function ($attribute, $value, $parameters, $validator) {
            $property = Property::find(request('id'));

            if ( ! $property) {
                return true;
            }

            foreach ($property->blockedDates()->get() as $booking) {
                if (
                    (Carbon::parse(request()->dates['from']) <= Carbon::parse($booking->departure)) &&
                    (Carbon::parse(request()->dates['to']) >=  Carbon::parse($booking->arrival))
                ) {
                    return false;
                }
            }

            return true;
        });

Please or to participate in this conversation.