TL; DR:为数组中的键寻找正则表达式或通配符表达式。
subs.0.worker_id
subs.1.worker_id
subs.2.worker_id
...
嗨,
我通过通配符唯一规则验证带有工作对象的数组:
'subs.*.worker_id'=>[
Rule::unique('teaching_units')->ignore($id,'teaching_unit_id')->where(function ($query) use ($date,$tu,$request,$subids) {
return $query->where(
[
['tu_date','=',$date],
['grid_unit_id','=',$tu['grid_unit_id']],
]
)
->whereIn('worker_id',$subids)
->exists();
})
],
此后,我想通过检查验证者及其错误来创建一条与此规则相关的消息。因此,通常我使用if (isset($failedRules['worker_id']['Unique']))
,但这不能与通配符一起使用。
foreach ($validator->errors()->toArray() as $error => $value) {
...
$failedRules = $validator->failed();
elseif (isset($failedRules['subs.*.worker_id']['Unique'])) {
$timetablesWithDuplicateTeachingUnits = DB::table('teaching_units')
->whereNotIn('teaching_unit_id', [$tu['teaching_unit_id']])
->where(
[
['tu_date','=',$date],
['grid_unit_id','=',$tu['grid_unit_id']],
]
)
->whereIn('worker_id',$subids)
->join('timetables', 'teaching_units.timetable_id', '=', 'timetables.timetable_id')
->join('courses', 'timetables.course_id', '=', 'courses.course_id')
->join('locations', 'courses.location_id', '=', 'locations.location_id')
->get();
$str = '';
foreach ($timetablesWithDuplicateTeachingUnits as $item) {
$str .= 'Im Kurs ' . $item->course_name . ' am Standort ' . $item->city . ' ist der Lehrer ' . $item->name . ' bereits für eine Stunde eingeteilt. ';
}
$errors->push([
'arrayId' => $tu['arrayId'],
//'field' => $error,
'field' => 'subs',
'msg' => $str,
]);
}
...
我知道subs.*.worker_id
无法正常工作。但这就是我想要得到的点。因此,我实际上要查找的是字段名称中的通配符/正则表达式。
谢谢。
寻找了preg_match
,但不知道这是否适用于Laravels failedRules。