我有一个时间跟踪应用程序,每次要添加一个新的时间条目时,我必须首先验证之前的所有时间条目是否都已关闭(意味着已设置结束日期),并使用validate()方法抛出错误消息.
我不知道这有多可行,也不知道如何做到,阅读文档似乎大多数定制规则都要求提供属性,但在这种情况下,更多的是验证逻辑需求,而不是post请求的形式.
当我收到一个post请求时,我会获取所有在post请求开始时间之前的、尚未给出结束时间的时间条目.
理想情况下,如果我得到任何返回的时间条目,我会抛出一个错误,说"你需要在打开一个新的时间条目之前关闭上一个时间条目".
为了更清楚,我想在代码中做以下几点:
$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
]);