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

trifek's avatar

Validate date in Laravel

Hi, I am beginner in Laravel. I make my project in Laravel 6.

I have this code:

public function validation(Request $request)
    {
        if ($this->endDate < now()->toDateString() or $this->summaryPeopleLimit <= $this->peopleLimit + 1) {
            return json_encode(['status' => 'error', 'message' => 'Rejestracja na to wydarzenie została zakończona!']);
        }


        if ($this->calcutateAge($request->input('date')) <= 18) {
            return json_encode(['status' => 'error', 'message' => 'Jesteś osobą niepełnoletnią. Rejestracja dozwolona jest dla użytkowników pełnoletnich!']);
        }

        if ($this->reservationRepository->isFreeReservationForEmail( $request->input('email'),$request->input('date') )) {
            return json_encode(['status' => 'error', 'message' => 'Posiadasz już rezerwację na to wydarzenie!']);
        }

        return json_encode(['status' => 'ok']);
    }

I need add this function: 1.Check if the date format: $ request-> input ('date') is correct (is it not, for example: 9999-99-99), 2. Check that the date in $ request-> input ('date') is not in the future

How can I make it?

0 likes
3 replies
trifek's avatar

you think about something like this:

$request->validate([
            'mydate' => 'date',
            'mydate' => 'date_format:y-m-d|before:today'
        ]);

??

Please or to participate in this conversation.