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

LdCoding's avatar

How to validate a post request without a specific attribute to validate.

I have a time tracking application where every time that a new Time Entry is about to be added, I must first verify that all the previous time entries have been closed (meaning that an ending date has been set) and throw and error message using the validate() method.

I don't know how feasable this is or how to do it, reading the documentation it seems that most custome rules require that an attribute be given, but in this case it's more about validating the logical requirements rather than the form of the post request.

When I receive a post request I fetch all previous time entries which come before the post request starting time and have not yet been given an ending time.

Ideally, if I get any time entries returned I would throw an error saying 'You need to close the previous time entry before opening a new one'.

For more clarity, here is what I want to do in code :

$timeEntry= new TimeEntry;
$openTimeEntries = $timeEntry->Where('start_time', '<', $request->startTime)->Where('end_time', 0)->get();
$count = $openTimeEntries->count();

$request->validate([
    'comment' => 'string',
    'candidateId' => 'required',
    'startTime' => 'required|date',
    'endTime' => 'date|nullable|after:startTime',
    'CustomeTimeEntryRule' => $openTimeEntries->count() > 0, // If false I want this rule to add the message to the validate error array
]);
0 likes
3 replies
Snapey's avatar

You can throw a separate validation exception, before or after checking the fields.

        if($openTimeEntries->count() > 0) {
            throw ValidationException::withMessages([
                'Incomplete' => ['You still have open time entries],
            ]);
        }
1 like

Please or to participate in this conversation.