From 556ffc52a78f48dfac1b297d38aabc0667f2b2ce Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Wed, 4 Nov 2026 16:50:33 +0100 Subject: [PATCH] fix(submission): use the same answer checks for validating and storing Validating an answer and storing it each had their own checks for the option ids of a question and for the answer "Other". Both now use the same helpers: the answer "Other" is recognized in one place, and option ids are read by one function, which takes a number or a string of its digits, as the API documentation now says. The message for an answer to a question that is not part of the form names the id as it was sent. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 2 +- lib/Controller/ApiController.php | 2 +- lib/Service/SubmissionService.php | 31 ++++++++------ tests/Unit/Service/SubmissionServiceTest.php | 44 ++++++++++++++++++++ 4 files changed, 64 insertions(+), 15 deletions(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index 9ca1e2752..6e7654fe4 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -950,7 +950,7 @@ Store Submission to Database - Restrictions: The array of answers has the following structure: - QuestionID as key - An **array** of values as value --> Even for short Text Answers, wrapped into Array. - - For Question-Types with pre-defined answers (`multiple`, `multiple_unique`, `dropdown`), the array contains the corresponding option-IDs. + - For Question-Types with pre-defined answers (`multiple`, `multiple_unique`, `dropdown`), the array contains the corresponding option-IDs, each as a number or as a string of exactly its digits, e.g. `12` or `"12"`, but not `"012"` or `" 12"`. - For File-Uploads, the array contains objects with `uploadedFileId` and `uploadToken` (both from the [Upload a file] endpoint for the same `questionId`). ``` diff --git a/lib/Controller/ApiController.php b/lib/Controller/ApiController.php index 045485b75..7628137a0 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -1931,7 +1931,7 @@ private function storeAnswersForQuestion(Form $form, $submissionId, array $quest $optionIndex = array_search($answer, array_column($question['options'], 'id')); if ($optionIndex !== false) { $answerText = $question['options'][$optionIndex]['text']; - } elseif (!empty($question['extraSettings']['allowOtherAnswer']) && str_starts_with($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX)) { + } elseif (SubmissionService::isOtherAnswer($question, $answer)) { $answerText = substr($answer, strlen(Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX)); } } elseif ($question['type'] === Constants::ANSWER_TYPE_FILE) { diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 237abbf74..d6357da0c 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -699,7 +699,7 @@ public function validateSubmission(array $questions, array $answers, string $for } // Check if all answers are within the possible options - if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED) && empty($question['extraSettings']['allowOtherAnswer'])) { + if (in_array($question['type'], Constants::ANSWER_TYPES_PREDEFINED)) { // Normalize option IDs once for consistent comparison (DB may return ints, request may send strings) $optionIds = $this->normalizeOptionIds($question['options'] ?? []); @@ -717,18 +717,11 @@ public function validateSubmission(array $questions, array $answers, string $for $this->validateGridQuestion($question, $answers[$questionId]); } // Search corresponding option, return false if non-existent - else { - // Accept numeric strings like "46" from JSON payloads reliably (e.g. with hardening extensions enabled) - $answerId = is_int($answer) ? $answer : (is_string($answer) ? intval(trim($answer)) : null); - - // Reject non-numeric / malformed values early - if ($answerId === null || (string)$answerId !== (string)intval($answerId)) { + elseif (!self::isOtherAnswer($question, $answer)) { + $answerId = $this->normalizeNumericOptionId($answer); + if ($answerId === null || !in_array($answerId, $optionIds, true)) { throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', is_scalar($answer) ? (string)$answer : gettype($answer), $question['text'])); } - - if (!in_array($answerId, $optionIds, true)) { - throw new \InvalidArgumentException(sprintf('Answer "%s" for question "%s" is not a valid option.', $answer, $question['text'])); - } } } } @@ -792,8 +785,8 @@ public function validateSubmission(array $questions, array $answers, string $for // Check for excess answers foreach ($answers as $id => $answerArray) { // Search corresponding question, return false if not found - if (!in_array($id, array_column($questions, 'id'))) { - throw new \InvalidArgumentException(sprintf('Answer for non-existent question with ID %d.', $id)); + if (!in_array($id, array_column($questions, 'id'), true)) { + throw new \InvalidArgumentException(sprintf('Answer for non-existent question with ID %s.', $id)); } } } @@ -934,6 +927,18 @@ private function validateGridQuestion(array $question, array $answers): void { } } + /** + * Whether the value is the answer "Other" of a question that allows it + * + * @param array $question the question + * @param mixed $answer a value of the answer + */ + public static function isOtherAnswer(array $question, mixed $answer): bool { + return !empty($question['extraSettings']['allowOtherAnswer']) + && is_string($answer) + && str_starts_with($answer, Constants::QUESTION_EXTRASETTINGS_OTHER_PREFIX); + } + /** * Whether the answer is a list of values: texts or numbers, uploaded files for file questions, or an object with the values by row for grid questions * diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index dffeb0d44..42d10b016 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -1058,6 +1058,50 @@ public static function dataValidateSubmission() { // Expected Result 'Answer for non-existent question with ID 2.', ], + 'question-id-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['validationType' => 'email']] + ], + [ + '1.0' => ['not an email'] + ], + 'Answer for non-existent question with ID 1.0.', + ], + 'option-id-in-other-notation' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'options' => [ + ['id' => 3], + ['id' => 5] + ]], + ], + [ + '1' => ['03'] + ], + 'Answer "03" for question "q1" is not a valid option.', + ], + 'option-id-in-other-notation-with-other-answer' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [ + ['id' => 3], + ['id' => 5] + ]], + ], + [ + '1' => ['05'] + ], + 'Answer "05" for question "q1" is not a valid option.', + ], + 'unknown-option-with-other-answer' => [ + [ + ['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => true, 'extraSettings' => ['allowOtherAnswer' => true], 'options' => [ + ['id' => 3] + ]], + ], + [ + '1' => ['not an option'] + ], + 'Answer "not an option" for question "q1" is not a valid option.', + ], 'invalid-multiple-too-many-answers' => [ // Questions [