diff --git a/app/Http/Requests/StudentUpdateRequest.php b/app/Http/Requests/StudentUpdateRequest.php index 2f6314f..283769e 100644 --- a/app/Http/Requests/StudentUpdateRequest.php +++ b/app/Http/Requests/StudentUpdateRequest.php @@ -6,7 +6,6 @@ use App\StudentData; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; -use Illuminate\Support\Str; class StudentUpdateRequest extends FormRequest { @@ -52,7 +51,7 @@ public function rules(): array ], 'research_profile.lang_proficiency' => 'sometimes|array', 'research_profile.lang_proficiency.*' => 'string|in:limited,basic,professional,native', - 'research_profile.*' => $this->validateCustomQuestions($schools), + 'research_profile.*' => $this->validateCustomQuestions(), 'research_profile.graduation_date' => 'required|date|after:today', 'research_profile.credit' => 'required|numeric|between:-1,1', ]; @@ -98,38 +97,56 @@ public function attributes() ]; } - public function validateCustomQuestions() + /** + * Check if attr is a custom question and validates the value based on the question type. + * Supported question types: + * - `yes_no`: Validates that the value is either "1" or "0". + * - Other types: Ensures the value does not exceed 1000 characters if it is a string. + * + * @return \Closure A validation closure to be used in validation rules. + */ + public function validateCustomQuestions() { return function($attr, $value, $fail) { + $errors = []; + $questions = StudentData::customQuestions()->flatten(1); $question_name = substr($attr, strpos($attr, '.') + 1); - $question_settings = $questions->first(function ($q) use ($question_name) { - return isset($q['name'], $q['type']) && $q['name'] === $question_name; - }); + $question_settings = $questions->first(fn($q) => isset($q['name'], $q['type']) && $q['name'] === $question_name); if (is_null($question_settings)) { - return; + return false; } - foreach ($value as $question => $answer) { - - switch ($question_settings['type']) { - case 'yes_no': - if (!in_array($answer, ["1", "0"])) { - $formated_question_name = strtolower(str_replace('_', ' ', $question)); - $fail("The answer to {$formated_question_name} must be Yes or No."); - } - break; - default: - if (strlen($answer) > 1000) { - $formated_question_name = strtolower(str_replace('_', ' ', $question)); - $fail("The value for the {$formated_question_name} question cannot exceed 1000 characters."); + switch ($question_settings['type']) { + case 'yes_no': + if (!in_array($value, ["1", "0"], true)) { + $formatted_question_name = $this->formatQuestionName($question_name); + $errors[] = "The answer to {$formatted_question_name} must be Yes or No."; + } + break; + default: + if (is_string($value) && strlen($value) > 1000) { + $formatted_question_name = $this->formatQuestionName($question_name); + $errors[] = "The value for the {$formatted_question_name} question cannot exceed 1000 characters."; } break; - } + } + + foreach ($errors as $error) { + $fail($error); } }; } + + /** + * Helper function to format question name + * @param string $question_name + * @return string + */ + function formatQuestionName($question_name) { + return strtolower(str_replace('_', ' ', $question_name)); + } }