|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Http\Requests; |
| 4 | + |
| 5 | +use App\Student; |
| 6 | +use App\StudentData; |
| 7 | +use Illuminate\Foundation\Http\FormRequest; |
| 8 | +use Illuminate\Validation\Rule; |
| 9 | +use Illuminate\Support\Collection; |
| 10 | + |
| 11 | +class StudentUpdateRequest extends FormRequest |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * Get the validation rules that apply to the request. |
| 16 | + * |
| 17 | + * @return array |
| 18 | + */ |
| 19 | + public function rules(): array |
| 20 | + { |
| 21 | + $schools = Student::participatingSchools()->keys()->all(); |
| 22 | + $majors = StudentData::majors()->merge(['Other' => 'Other'])->all(); |
| 23 | + |
| 24 | + return [ |
| 25 | + 'full_name' => 'required|string', |
| 26 | + 'faculty.*' => [ |
| 27 | + 'integer', |
| 28 | + Rule::exists('profiles', 'id')->where('public', 1), |
| 29 | + ], |
| 30 | + 'research_profile.major' => [ |
| 31 | + 'sometimes', |
| 32 | + 'nullable', |
| 33 | + Rule::in($majors) |
| 34 | + ], |
| 35 | + 'research_profile.brief_intro' => 'sometimes|string', |
| 36 | + 'research_profile.intro' => 'sometimes|string', |
| 37 | + 'research_profile.interest' => 'sometimes|string', |
| 38 | + 'research_profile.schools' => 'required|array', |
| 39 | + 'research_profile.schools.*' => [ |
| 40 | + 'sometimes', |
| 41 | + 'string', |
| 42 | + Rule::in($schools), |
| 43 | + ], |
| 44 | + 'research_profile.availability' => 'sometimes|array', |
| 45 | + 'research_profile.semesters' => 'required|array', |
| 46 | + 'research_profile.semesters.*' => [ |
| 47 | + 'required', |
| 48 | + 'string', |
| 49 | + 'regex:/\b(Winter|Spring|Summer|Fall)\s\d{4}\b/', |
| 50 | + ], |
| 51 | + 'research_profile.languages' => 'sometimes|array', |
| 52 | + 'research_profile.languages.*' => [ |
| 53 | + Rule::in(array_keys(StudentData::$languages)), |
| 54 | + ], |
| 55 | + 'research_profile.lang_proficiency' => 'sometimes|array', |
| 56 | + 'research_profile.lang_proficiency.*' => 'string|in:limited,basic,professional,native', |
| 57 | + 'research_profile.graduation_date' => 'required|date|after:today', |
| 58 | + 'research_profile.credit' => 'required|numeric|in:-1,0,1', |
| 59 | + ] + $this->customQuestionRules(); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Get the error messages for the defined validation rules. |
| 64 | + * |
| 65 | + * @return array |
| 66 | + */ |
| 67 | + public function messages() |
| 68 | + { |
| 69 | + return [ |
| 70 | + 'faculty.*.exists' => 'One or more of your selected faculty members are not available. Please review and update your selections.', |
| 71 | + 'faculty.required' => 'You must select at least one faculty member that you would like to work with.', |
| 72 | + 'research_profile.major.required' => 'The major field is required.', |
| 73 | + 'research_profile.semesters.required' => 'At least one semester is required.', |
| 74 | + 'research_profile.schools.required' => 'At least one school is required.', |
| 75 | + 'research_profile.schools.*.in' => 'Wrong value :input. Please select a valid school.', |
| 76 | + 'research_profile.semesters.*.regex' => 'Wrong value selected for semester. The semester must follow the "Season YYYY" format.', |
| 77 | + 'research_profile.languages.*.in' => 'Wrong value :input selected for language. Please select a valid language.', |
| 78 | + 'research_profile.graduation_date.after' => 'The graduation date must be in the future.', |
| 79 | + 'research_profile.credit.between' => 'The credit value is invalid.', |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Get custom attributes for validator errors. |
| 85 | + * |
| 86 | + * @return array |
| 87 | + */ |
| 88 | + public function attributes() |
| 89 | + { |
| 90 | + return [ |
| 91 | + 'full_name' => 'display name', |
| 92 | + 'research_profile.major' => 'major', |
| 93 | + 'research_profile.semesters' => 'semesters', |
| 94 | + 'research_profile.schools' => 'school', |
| 95 | + 'research_profile.graduation_date' => 'graduation date', |
| 96 | + 'research_profile.credit' => 'credit', |
| 97 | + 'research_profile.brief_intro' => 'research opportunity reasons', |
| 98 | + 'research_profile.intro' => 'future goals', |
| 99 | + 'research_profile.interest' => 'interest', |
| 100 | + ] + $this->customQuestionAttributes(); |
| 101 | + } |
| 102 | + |
| 103 | + public function customQuestions(): Collection |
| 104 | + { |
| 105 | + return StudentData::customQuestions()->flatten(1); |
| 106 | + } |
| 107 | + |
| 108 | + public function customQuestionRules(): array |
| 109 | + { |
| 110 | + return $this->customQuestions()->mapWithKeys(function($question, $key) { |
| 111 | + return [ |
| 112 | + "research_profile.{$question['name']}" => match ($question['type']) { |
| 113 | + 'yes_no' => 'sometimes|boolean', |
| 114 | + 'text' => 'sometimes|nullable|string|max:256', |
| 115 | + 'textarea' => 'sometimes|nullable|string', |
| 116 | + default => 'sometimes|nullable', |
| 117 | + } |
| 118 | + ]; |
| 119 | + })->all(); |
| 120 | + } |
| 121 | + |
| 122 | + public function customQuestionAttributes(): array |
| 123 | + { |
| 124 | + return $this->customQuestions()->mapWithKeys(function($question, $key) { |
| 125 | + return [ |
| 126 | + "research_profile.{$question['name']}" => "{$question['school']} research " . str_replace('_', ' ', $question['name']) |
| 127 | + ]; |
| 128 | + })->all(); |
| 129 | + } |
| 130 | + |
| 131 | +} |
0 commit comments