vendredi 21 décembre 2018

Laravel FailedRules Wildcard Regular Expression [Objects in Array]

TL;DR: Looking for a regular expression or wildcard expression for keys in an array.

subs.0.worker_id
subs.1.worker_id
subs.2.worker_id
...

Hi,

I validate an array with worker objects over a wildcard unique rule:

'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();
})
],

After this I want to create an individual message to this rule by checking the validator and his errors. So normally I use if (isset($failedRules['worker_id']['Unique'])) but thats not properly working with wildcards.

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,
            ]);
}
...

I know subs.*.worker_id is not working. But thats the point where i want to get. So what I am actually looking for is a wildcard / regular expression in the field name.

Thanks in advance.

Looked for preg_match but dont know if that will work with Laravels failedRules.



from Newest questions tagged laravel-5 - Stack Overflow http://bit.ly/2AdOPN5
via IFTTT

Aucun commentaire:

Enregistrer un commentaire