Skip to content

Commit 70a7f6f

Browse files
betsyecastrowunc
andauthored
Adds Student Research Form Validations (#166)
* 🦺 Adds student research form validations --------- Co-authored-by: Wun Chiou <wun@utdallas.edu>
1 parent 74eeeda commit 70a7f6f

File tree

3 files changed

+144
-2
lines changed

3 files changed

+144
-2
lines changed

app/Http/Controllers/StudentsController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace App\Http\Controllers;
44

55
use App\Events\StudentViewed;
6+
use App\Http\Requests\StudentUpdateRequest;
67
use App\Student;
78
use App\StudentData;
89
use App\User;
@@ -50,7 +51,7 @@ public function index(): View|ViewContract
5051
/**
5152
* Create a new student research application.
5253
*/
53-
public function create(Request $request): RedirectResponse
54+
public function create(StudentUpdateRequest $request): RedirectResponse
5455
{
5556
$student = $request->user()->studentProfiles->first() ?? $this->store($request);
5657

@@ -127,7 +128,7 @@ public function edit(Student $student): View|ViewContract
127128
/**
128129
* Update the specified student research application in the database.
129130
*/
130-
public function update(Request $request, Student $student): RedirectResponse
131+
public function update(StudentUpdateRequest $request, Student $student): RedirectResponse
131132
{
132133
$updated = $student->update([
133134
'full_name' => $request->full_name,
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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+
}

resources/views/students/edit.blade.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@
2929
<h1 class="mb-0">Student Research Application</h1>
3030
<h2 class="mt-0 text-muted">for {{ $student->full_name }}</h2>
3131

32+
@if ($errors->any())
33+
<div class="alert alert-danger alert-dismissable" role="alert">
34+
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
35+
<p><strong>There are some errors. Please correct them and try again.</strong></p>
36+
@foreach ($errors->all() as $error)
37+
{{ $error }}<br>
38+
@endforeach
39+
</div>
40+
@endif
41+
3242
<div class="alert alert-success" role="alert">
3343
<p class="mb-0">Complete and submit your student research application below. This application will not be public, but will be made available to faculty researchers who may be looking for students. After submitting, you can always come back later to edit or withdraw your student research application.</p>
3444
</div>

0 commit comments

Comments
 (0)