Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/API_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

```
Expand Down
2 changes: 1 addition & 1 deletion lib/Controller/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
31 changes: 18 additions & 13 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'] ?? []);

Expand All @@ -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']));
}
}
}
}
Expand Down Expand Up @@ -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));
}
}
}
Expand Down Expand Up @@ -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
*
Expand Down
44 changes: 44 additions & 0 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
[
Expand Down
Loading