Skip to content

Commit

Permalink
validateCustomQuestions() method refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
betsyecastro committed Nov 21, 2024
1 parent 7930f9f commit 6b9dd8d
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions app/Http/Requests/StudentUpdateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
use App\StudentData;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Support\Str;

class StudentUpdateRequest extends FormRequest
{
Expand Down Expand Up @@ -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',
];
Expand Down Expand Up @@ -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));
}
}

0 comments on commit 6b9dd8d

Please sign in to comment.