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

tehcoder's avatar

Validate if input date is before the date stored in db

I want to check if an Event Registration is still available at the selected input date.

I store the expiration_date in the column date_end, table events.

In my EventSignupRequest I tried this, but it won't validate any date now.

'date' => 'required|date|after:yesterday|before_or_equal:events,date_end',
// returns
The date must be a date before or equal to events.

I found a workaround and added a separate manual validation before I store the data, but I would like to have all the code in EventSignupRequest.

Thank you!

0 likes
3 replies
Aforeg's avatar
Aforeg
Best Answer
Level 1

You could use withValidator in your Request.

public function withValidator($validator) {
    $validator->after(function ($validator) {
        $event == Event::find($this->input('event_id'))->first();
        if($event->date_end < $this->input('registration_date')) {
            $validator->errors()->add('event_id', 'Sorry, registration for this event has expired');
        }
    });
}
tehcoder's avatar

Awesome! I didn't know about withValidator function. Thanks for help!

PS: there is a syntax error, make sure you close after();

Please or to participate in this conversation.