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
1 change: 1 addition & 0 deletions docs/API_v3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`).

```
Expand Down
5 changes: 3 additions & 2 deletions lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Comment thread
AndyScherzinger marked this conversation as resolved.
Resolved
Comment thread
AndyScherzinger marked this conversation as resolved.
Resolved
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
Expand Down
22 changes: 22 additions & 0 deletions playwright/e2e/submit-form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
13 changes: 8 additions & 5 deletions src/views/Submit.vue
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Comment thread
AndyScherzinger marked this conversation as resolved.
Resolved
.filter((choice) => /^\d+$/.test(String(choice)))
.map((choice) => String(Number(choice)))
Comment thread
AndyScherzinger marked this conversation as resolved.
Resolved
.filter(
(choice) =>
Number(choice) >= lowest && Number(choice) <= highest,
)
}
if (!answerTypes[question.type]?.predefined) {
return value
Expand Down
46 changes: 44 additions & 2 deletions tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
[
Expand All @@ -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' => [
[
Expand All @@ -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' => [
Comment thread
AndyScherzinger marked this conversation as resolved.
Resolved
// Questions
[
Expand Down
Loading