php - Laravel validator 'in' issue for numbers in input -
i need in fixing issue in using laravel 'in' validator match given input against multiple comma separated string of values. issue when there number in input , not matches of comma separated values gives exception:
preg_match() expects parameter 2 string, array given
however should give error message in validator object input field not match. instead gives above mentioned exception. following code:
if (!empty($program)) { $institution_id = $program->institutionid; $roster_users = $program->usersrosters()->where('profiletype', 'student')->get(); if (!empty($roster_users)) { $rostered_users_ids = implode(',', $roster_users->lists('id')); } if (!empty($roster_users)) { $rostered_users_usernames = implode(',', $roster_users->lists('username')); } $teacher_roster_users = $program->usersrosters()->where('profiletype', 'external staff')->get(); if (!empty($teacher_roster_users)) { $teacher_rostered_users_usernames = implode(',', $teacher_roster_users->lists('username')); } if (!empty($teacher_roster_users)) { $teacher_rostered_users_data = $teacher_roster_users->lists('id', 'username'); } } $rules = [ 'aasectionname.required' => '501 – aa section not exist', 'aaid' => 'numeric|exists:users,id|in:' . $rostered_users_ids . '|aaidinstitutionmismatch:' . $institution_id, 'institutionid' => 'numeric|in:' . $institution_id, 'username' => 'exists:users,username|in:' . $rostered_users_usernames . '|usernameinstitutionmismatch:' . $institution_id, 'schoolid' => 'numeric', 'groupowner' => 'in:'.$teacher_rostered_users_usernames, 'remove' => 'in:0,1' ]; $messages = [ // institutionid 'institutionid.numeric' => '101 – invalid institution id', 'institutionid.in' => '102 – institution id not match program', // usernames 'username.exists' => '201 – username not exist', 'username.in' => '202 – username not rostered program', 'username.usernameinstitutionmismatch' => '203 – user/institution mismatch - username not assigned institution', // aaid 'aaid.numeric' => '301 – aa id not exist', 'aaid.exists' => '301 – aa id not exist', 'aaid.in' => '302 – aa id not rostered program', 'aaid.aaidinstitutionmismatch' => '303 – aa id/institution mismatch – aaid not assigned institution', // mismatch 'bothmismatch' => '401 – aaid/username/schoolid not match (this combination of @ least 2 of these items)', // teacher 'groupowner.in' => '501 – groupowner not exist', // remove 'remove' => '601 – no student record match remove', ]; excel::load($file, function($excel) use($program, $rules, $messages, $errors, &$errors_data, $teacher_rostered_users_data) { global $totalmismatch; $results = $excel->get(); $program_group_model = new programgroup; $option_model = new option; $group_default_status_id = key($option_model->getprogramsstatus(['active'])); $groupvisibilitystatusid = $group_type = $option_model->getvisibilityoptions('question visibility','private'); $data = []; $lastsecid = null; $groupsectionpreviousname = null; $groupname = null; $data['status'] = $group_default_status_id; $data['program_id'] = $program->id; $groupname_lists = $program_group_model->with(['programs' => function ($query) use ($program){ $query->where('programid','=',$program->id); }])->get()->lists('name','id'); foreach ($results $key => $row) { $inputs = $row->toarray(); $groupname = trim($inputs['usergroup']); $errors = []; // stop reading excel when aasectionname empty if (empty($groupname) || $groupname == null) { $errors['remove'] = $messages['aasectionname.required']; } $validator = validator::make($inputs, $rules, $messages); if ($validator->fails()) { $errors = $validator->messages()->toarray(); foreach ($errors $error) { foreach ($error $e) { $errors_data[] = [$key + 2, $e]; } } } else { $aaid = intval($inputs['aaid']); $groupowner_name = $inputs['groupowner']; if (!empty($teacher_rostered_users_data[$groupowner_name])) { $groupowner_id = $teacher_rostered_users_data[$groupowner_name]; $data['owner'] = $groupowner_id; } $remove = intval($inputs['remove']); // remove existing student roster if (!empty($remove)) { $removed = false; $user_ids = is_array($aaid) ? $aaid : [$aaid]; $program_group = $program->programgroups()->where('name', $groupname); if (!empty($program_group)) { $program_group = $program_group->first(); } if (!empty($program_group)) { $program_group = $program_group->users(); } if (!empty($program_group) && !empty($user_ids)) { $removed = $program_group->detach($user_ids); } if (!$removed) { $errors['remove'] = $messages['remove']; } } else { if (!in_array($groupname, $groupname_lists) || $groupsectionpreviousname != $groupname) { $data['name'] = $groupname; $data['group_id'] = array_search($groupname, $groupname_lists); $data['group_type'] = $groupvisibilitystatusid; $sectionid = $program_group_model->saveprogramgroup($data); $data[$sectionid]['selected'][] = $aaid; $groupname_lists[$sectionid] = $groupname; } else { $temp = array_flip($groupname_lists); $data[$temp[$groupname]]['selected'][] = $aaid; } } if ($totalmismatch === 2) { $errors['bothmismatch'] = $messages['bothmismatch']; } foreach ($errors $error) { $errors_data[] = [$key + 2, $error]; } } $groupsectionpreviousname = $groupname; } $programaasectionmodelforsectionusers = new programsectionuser(); $programaasectionmodelforsectionusers->saveprogramgroupusers($data); });
$rules
array:7 [ "aasectionname.required" => "501 – aa section not exist" "aaid" => "numeric|exists:users,id|in:28,29,32,33,25,24,27|aaidinstitutionmismatch:42" "institutionid" => "numeric|in:42" "username" => "exists:users,username|in:kabeer,ayaz,fddesaaweqq,fdawerascvdfc,haseeb,kamran,shahid|usernameinstitutionmismatch:42" "schoolid" => "numeric" "groupowner" => "in:externalstaff,rahat,uzma,sahar,haseebahmad,saimariaz,fredrick" "remove" => "in:0,1" ]
Comments
Post a Comment