Skip to content

Commit

Permalink
Refactor AnswerFormRequest to enhance option matching logic
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
JhumanJ committed Nov 29, 2024
1 parent 13c40f6 commit 6cf1cd6
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions api/app/Http/Requests/AnswerFormRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']];
}
Expand Down

0 comments on commit 6cf1cd6

Please sign in to comment.