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.
This commit is contained in:
Julien Nahum 2024-11-29 15:17:49 +01:00
parent 13c40f6e54
commit 6cf1cd663a
1 changed files with 2 additions and 5 deletions

View File

@ -67,18 +67,15 @@ class AnswerFormRequest extends FormRequest
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']];
}