From 3b2e6254b8a24f6b105b90d6732f86660355386e Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Sat, 21 Nov 2026 16:36:04 +0100 Subject: [PATCH] fix(submission): accept a linear scale answer only as its plain number The check of a linear scale answer accepted "05" and the JSON number 4.0, as ctype_digit() only looked at the digits of the value cast to a string. "05" was stored as sent, so the results counted it as a value of its own next to "5", and exports showed it as written. 4.0 was stored as "4". The check now reads the answer with normalizeNumericOptionId(), so a linear scale answer follows the same plain-number rule as the option ids of choice questions: a number, or a string of exactly its digits. For API clients that send "05" or 4.0 this changes behaviour: they get an error that names the rule. Answers already stored are not affected. The submit page restores a scale answer from a draft or from an edited response as its plain number as well, so an answer stored as "05" before is sent back as "5" instead of being rejected. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/API_v3.md | 1 + lib/Service/SubmissionService.php | 5 ++- playwright/e2e/submit-form.spec.ts | 22 ++++++++++ src/views/Submit.vue | 13 +++--- tests/Unit/Service/SubmissionServiceTest.php | 46 +++++++++++++++++++- 5 files changed, 78 insertions(+), 9 deletions(-) diff --git a/docs/API_v3.md b/docs/API_v3.md index a06972f8b..7c0b0fb82 100644 --- a/docs/API_v3.md +++ b/docs/API_v3.md @@ -953,6 +953,7 @@ Store Submission to Database - 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, each as a number or as a string of exactly its digits, e.g. `12` or `"12"`, but not `"012"` or `" 12"`. + - For linear scale questions (`linearscale`), the array contains one value between `optionsLowest` and `optionsHighest`, as a number or as a string of exactly its digits, e.g. `4` or `"4"`, but not `"04"` or `4.0`. - 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/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 0678b7ad8..78e4a6437 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -708,8 +708,9 @@ public function validateSubmission(array $questions, array $answers, string $for if ($question['type'] === Constants::ANSWER_TYPE_LINEARSCALE) { $optionsLowest = $question['extraSettings']['optionsLowest'] ?? 1; $optionsHighest = $question['extraSettings']['optionsHighest'] ?? 5; - if (!is_scalar($answer) || is_bool($answer) || !ctype_digit((string)$answer) || intval($answer) < $optionsLowest || intval($answer) > $optionsHighest) { - throw new \InvalidArgumentException(sprintf('The answer for question "%s" must be an integer between %d and %d.', $question['text'], $optionsLowest, $optionsHighest)); + $value = $this->normalizeNumericOptionId($answer); + if ($value === null || $value < $optionsLowest || $value > $optionsHighest) { + throw new \InvalidArgumentException(sprintf('The answer for question "%s" must be a whole number from %d to %d, written without leading zeros or a decimal point.', $question['text'], $optionsLowest, $optionsHighest)); } } // Check if all grid rows, columns and values match the configured grid subtype diff --git a/playwright/e2e/submit-form.spec.ts b/playwright/e2e/submit-form.spec.ts index 04d521640..ecc93edce 100644 --- a/playwright/e2e/submit-form.spec.ts +++ b/playwright/e2e/submit-form.spec.ts @@ -235,6 +235,28 @@ test('A restored draft leaves out values the questions no longer offer', async ( expect(answers[grid.id]).toEqual({ [rows[0].id]: [columns[0].id] }) }) +test('A restored draft sends a linear scale answer as its plain number', async ({ + page, + submitView, +}) => { + const { form, scale } = await createOfferedValuesForm(page.request) + await page.goto(`apps/forms/${form.hash}/submit`) + await page.evaluate( + ([key, draft]) => localStorage.setItem(key, draft), + [ + `nextcloud_forms_${form.hash}`, + JSON.stringify({ + [scale.id]: { value: ['05'], type: 'QuestionLinearScale' }, + }), + ], + ) + await page.reload() + + const response = await submitView.submit() + expect(response.status()).toBe(201) + expect(response.request().postDataJSON().answers[scale.id]).toEqual(['5']) +}) + test('An edited submission leaves out values the questions no longer offer', async ({ page, submitView, diff --git a/src/views/Submit.vue b/src/views/Submit.vue index 0701e3abe..b805eabaf 100644 --- a/src/views/Submit.vue +++ b/src/views/Submit.vue @@ -683,7 +683,7 @@ export default defineComponent({ } /** - * The values of an answer the question still offers: its options or an answer "Other" while it allows one, the rows and columns of a grid, the values within the bounds of a linear scale + * The values of an answer the question still offers: its options or an answer "Other" while it allows one, the rows and columns of a grid, the values within the bounds of a linear scale as their plain number * * @param question the question * @param value the values of the answer @@ -700,10 +700,13 @@ export default defineComponent({ if (question.type === 'linearscale') { const lowest = Number(question.extraSettings?.optionsLowest ?? 1) const highest = Number(question.extraSettings?.optionsHighest ?? 5) - return value.filter( - (choice) => - Number(choice) >= lowest && Number(choice) <= highest, - ) + return value + .filter((choice) => /^\d+$/.test(String(choice))) + .map((choice) => String(Number(choice))) + .filter( + (choice) => + Number(choice) >= lowest && Number(choice) <= highest, + ) } if (!answerTypes[question.type]?.predefined) { return value diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index 8771f47bf..bb0c05cd7 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -1451,7 +1451,7 @@ public static function dataValidateSubmission() { '1' => ['6'] ], // Expected Result - 'The answer for question "q1" must be an integer between 0 and 5.', + 'The answer for question "q1" must be a whole number from 0 to 5, written without leading zeros or a decimal point.', ], 'boolean-linearscale-answer' => [ [ @@ -1460,7 +1460,7 @@ public static function dataValidateSubmission() { [ '1' => [true] ], - 'The answer for question "q1" must be an integer between 0 and 5.', + 'The answer for question "q1" must be a whole number from 0 to 5, written without leading zeros or a decimal point.', ], 'list-linearscale-answer' => [ [ @@ -1471,6 +1471,48 @@ public static function dataValidateSubmission() { ], 'Invalid answer for question "q1".', ], + 'valid-linearscale-answers' => [ + [ + ['id' => 1, 'type' => 'linearscale', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['optionsLowest' => 0]], + ['id' => 2, 'type' => 'linearscale', 'text' => 'q2', 'isRequired' => false, 'extraSettings' => ['optionsLowest' => 0]], + ['id' => 3, 'type' => 'linearscale', 'text' => 'q3', 'isRequired' => false, 'extraSettings' => ['optionsHighest' => 10]], + ['id' => 4, 'type' => 'linearscale', 'text' => 'q4', 'isRequired' => false], + ], + [ + '1' => ['0'], + '2' => [0], + '3' => ['10'], + '4' => [5], + ], + null, + ], + 'zero-linearscale-answer-below-default-lowest' => [ + [ + ['id' => 1, 'type' => 'linearscale', 'text' => 'q1', 'isRequired' => false] + ], + [ + '1' => ['0'] + ], + 'The answer for question "q1" must be a whole number from 1 to 5, written without leading zeros or a decimal point.', + ], + 'leading-zero-linearscale-answer' => [ + [ + ['id' => 1, 'type' => 'linearscale', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['optionsLowest' => 0]] + ], + [ + '1' => ['05'] + ], + 'The answer for question "q1" must be a whole number from 0 to 5, written without leading zeros or a decimal point.', + ], + 'float-linearscale-answer' => [ + [ + ['id' => 1, 'type' => 'linearscale', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['optionsLowest' => 0]] + ], + [ + '1' => [4.0] + ], + 'The answer for question "q1" must be a whole number from 0 to 5, written without leading zeros or a decimal point.', + ], 'invalid-color-question' => [ // Questions [