Skip to content

Commit eb66419

Browse files
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. Such an answer was stored as sent, so the results counted "05" as a value of its own next to "5", and exports showed it as written. The check now reads the answer with normalizeNumericOptionId(), like the option ids of choice questions: a number, or a string of exactly its digits. 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 <info@andy-scherzinger.de>
1 parent 365deea commit eb66419

4 files changed

Lines changed: 28 additions & 5 deletions

File tree

‎docs/API_v3.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -953,6 +953,7 @@ Store Submission to Database
953953
- QuestionID as key
954954
- An **array** of values as value --> Even for short Text Answers, wrapped into Array.
955955
- 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"`.
956+
- 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`.
956957
- For File-Uploads, the array contains objects with `uploadedFileId` and `uploadToken` (both from the [Upload a file] endpoint for the same `questionId`).
957958

958959
```

‎lib/Service/SubmissionService.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,8 @@ public function validateSubmission(array $questions, array $answers, string $for
708708
if ($question['type'] === Constants::ANSWER_TYPE_LINEARSCALE) {
709709
$optionsLowest = $question['extraSettings']['optionsLowest'] ?? 1;
710710
$optionsHighest = $question['extraSettings']['optionsHighest'] ?? 5;
711-
if (!is_scalar($answer) || is_bool($answer) || !ctype_digit((string)$answer) || intval($answer) < $optionsLowest || intval($answer) > $optionsHighest) {
711+
$value = $this->normalizeNumericOptionId($answer);
712+
if ($value === null || $value < $optionsLowest || $value > $optionsHighest) {
712713
throw new \InvalidArgumentException(sprintf('The answer for question "%s" must be an integer between %d and %d.', $question['text'], $optionsLowest, $optionsHighest));
713714
}
714715
}

‎src/views/Submit.vue‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -700,10 +700,13 @@ export default defineComponent({
700700
if (question.type === 'linearscale') {
701701
const lowest = Number(question.extraSettings?.optionsLowest ?? 1)
702702
const highest = Number(question.extraSettings?.optionsHighest ?? 5)
703-
return value.filter(
704-
(choice) =>
705-
Number(choice) >= lowest && Number(choice) <= highest,
706-
)
703+
return value
704+
.filter((choice) => /^\d+$/.test(String(choice)))
705+
.map((choice) => String(Number(choice)))
706+
.filter(
707+
(choice) =>
708+
Number(choice) >= lowest && Number(choice) <= highest,
709+
)
707710
}
708711
if (!answerTypes[question.type]?.predefined) {
709712
return value

‎tests/Unit/Service/SubmissionServiceTest.php‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,6 +1471,24 @@ public static function dataValidateSubmission() {
14711471
],
14721472
'Invalid answer for question "q1".',
14731473
],
1474+
'leading-zero-linearscale-answer' => [
1475+
[
1476+
['id' => 1, 'type' => 'linearscale', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['optionsLowest' => 0]]
1477+
],
1478+
[
1479+
'1' => ['05']
1480+
],
1481+
'The answer for question "q1" must be an integer between 0 and 5.',
1482+
],
1483+
'float-linearscale-answer' => [
1484+
[
1485+
['id' => 1, 'type' => 'linearscale', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['optionsLowest' => 0]]
1486+
],
1487+
[
1488+
'1' => [4.0]
1489+
],
1490+
'The answer for question "q1" must be an integer between 0 and 5.',
1491+
],
14741492
'invalid-color-question' => [
14751493
// Questions
14761494
[

0 commit comments

Comments
 (0)