我希望您能够验证给定的讲座。有一些验证规则,但我想通过编写一个验证函数来扩展它,该函数将以3个属性作为参数,并检查数据库中的其他讲座,如果没有讲座重叠,则返回有效。讲座具有开始和结束的DateTime属性。
验证函数是:
我不知道在哪里放置此函数,以便Laravel的验证器抛出异常,并且我可以使用i18n指定错误消息。
谢谢。
验证函数是:
private function noOtherLecturesOnGivenTime($start, $end, $hall)
{
if(!$start instanceof Carbon) {
$start = new Carbon($start);
}
if(!$end instanceof Carbon) {
$end = new Carbon($end);
}
if(is_null($hall)) {
return true; // if there is no hall defined, all times are acceptable
}
if(!$hall instanceof Model) {
/** @var Hall $hall */
$hall = Hall::find($hall);
}
$overlappingLectures = $hall->lectures()->get()->filter(function ($lecture) use ($start, $end) {
// (Ts < e) and (Te > s)
return $start->lt($lecture->end) and $end->gt($lecture->start);
});
return $overlappingLectures->count() === 0;
}
我不知道在哪里放置此函数,以便Laravel的验证器抛出异常,并且我可以使用i18n指定错误消息。
谢谢。
$end
$end
$hall
吗?只需在自定义验证规则中获取你的请求以获取参数并使用你的函数。 - jeanj