diff --git a/docs/API_v3.md b/docs/API_v3.md index 019b2df8c..dfe00e412 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -942,7 +942,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 01d4eec4b..ad1abd736 100644 --- a/lib/Controller/ApiController.php +++ b/lib/Controller/ApiController.php @@ -1866,7 +1866,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 43dba65df..f1fc12c76 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -690,7 +690,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'] ?? []); @@ -708,18 +708,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'])); - } } } } @@ -783,8 +776,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)); } } } @@ -925,6 +918,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 17ce3fdfd..4f89a944e 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -946,6 +946,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 [