From 6cf1cd663a0a674a1ee744705cb30921a0afaada Mon Sep 17 00:00:00 2001 From: Julien Nahum Date: Fri, 29 Nov 2024 15:17:49 +0100 Subject: [PATCH] Refactor AnswerFormRequest to enhance option matching logic - Improved the option matching logic in AnswerFormRequest by ensuring both 'id' and 'name' are checked for validity before comparison. - Simplified the handling of selection fields and single select values to return the original value if no match is found. This change aims to increase the robustness of data handling in form requests. --- api/app/Http/Requests/AnswerFormRequest.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/api/app/Http/Requests/AnswerFormRequest.php b/api/app/Http/Requests/AnswerFormRequest.php index d9dec34f..d3bedae9 100644 --- a/api/app/Http/Requests/AnswerFormRequest.php +++ b/api/app/Http/Requests/AnswerFormRequest.php @@ -67,18 +67,15 @@ public function rules() foreach ($selectionFields as $field) { if (isset($data[$field['id']]) && is_array($data[$field['id']])) { $data[$field['id']] = array_map(function ($val) use ($field) { - // Find the option by exact ID match first $tmpop = collect($field[$field['type']]['options'])->first(function ($op) use ($val) { - return $op['id'] === $val || $op['name'] === $val; + return isset($op['id'], $op['name']) && ($op['id'] === $val || $op['name'] === $val); }); - - // Return the original value if no match found return isset($tmpop['name']) ? $tmpop['name'] : $val; }, $data[$field['id']]); } elseif (isset($data[$field['id']])) { // Handle single select values $tmpop = collect($field[$field['type']]['options'])->first(function ($op) use ($field, $data) { - return $op['id'] === $data[$field['id']] || $op['name'] === $data[$field['id']]; + return isset($op['id'], $op['name']) && ($op['id'] === $data[$field['id']] || $op['name'] === $data[$field['id']]); }); $data[$field['id']] = isset($tmpop['name']) ? $tmpop['name'] : $data[$field['id']]; }