From 16cb603be169bc56ee0767c36cfd1fb72fe5f544 Mon Sep 17 00:00:00 2001 From: Andy Scherzinger Date: Thu, 26 Nov 2026 00:30:45 +0100 Subject: [PATCH 1/6] feat(questions): limit the length of text answers and the range of numbers Form owners could only restrict text answers through a regular expression, which only short answers offer and which respondents never see. Google Forms and Microsoft Forms both let owners set a minimum and maximum character count and a number range, so answers fit where they are used later, for example in print. Short and long answers now take a minimum and a maximum length, and short answers validated as a number take a lowest and a highest number. Respondents see the rule as a hint above the field, a character counter below it, and the rule as error text when the answer breaks it. The browser stops typing at the maximum length, and the server rejects answers outside the limits. Assisted-by: ClaudeCode:claude-opus-5-5 Signed-off-by: Andy Scherzinger --- docs/DataStructure.md | 4 + lib/Constants.php | 9 + lib/ResponseDefinitions.php | 4 + lib/Service/FormsService.php | 53 +++++ lib/Service/SubmissionService.php | 28 +++ openapi.json | 32 +++ src/components/Questions/QuestionLong.vue | 113 ++++++++++- src/components/Questions/QuestionShort.vue | 175 ++++++++++++++++- src/composables/useAnswerLimits.ts | 193 +++++++++++++++++++ tests/Unit/Service/FormsServiceTest.php | 55 ++++++ tests/Unit/Service/SubmissionServiceTest.php | 35 ++++ 11 files changed, 688 insertions(+), 13 deletions(-) create mode 100644 src/composables/useAnswerLimits.ts diff --git a/docs/DataStructure.md b/docs/DataStructure.md index 2aabc65ef..772c32010 100644 --- a/docs/DataStructure.md +++ b/docs/DataStructure.md @@ -253,6 +253,10 @@ Optional extra settings for some [Question Types](#question-types) | `optionsLimitMin` | `multiple` | Integer | - | Minimum number of options that must be selected | | `validationType` | `short` | string | `null, 'phone', 'email', 'regex', 'number'` | Custom validation for checking a submission | | `validationRegex` | `short` | string | regular expression | if `validationType` is 'regex' this defines the regular expression to apply | +| `lengthMin` | `short, long` | Integer | `1` to `4096` | Minimum number of characters of the answer; for `short` only without `validationType` | +| `lengthMax` | `short, long` | Integer | `1` to `4096` | Maximum number of characters of the answer, not below `lengthMin`; for `short` only without `validationType` | +| `numberMin` | `short` | Number | - | Lowest number accepted, only with `validationType` 'number' | +| `numberMax` | `short` | Number | - | Highest number accepted, not below `numberMin`, only with `validationType` 'number' | | `allowedFileTypes` | `file` | Array of strings | `'image', 'x-office/document'` | Allowed file types for file upload | | `allowedFileExtensions` | `file` | Array of strings | `'jpg', 'png'` | Allowed file extensions for file upload | | `maxAllowedFilesCount` | `file` | Integer | - | Maximum number of files that can be uploaded, 0 means no limit | diff --git a/lib/Constants.php b/lib/Constants.php index f56977d8e..1c7ceb4dd 100644 --- a/lib/Constants.php +++ b/lib/Constants.php @@ -204,10 +204,19 @@ class Constants { ]; public const EXTRA_SETTINGS_SHORT = [ + 'lengthMax' => ['integer'], + 'lengthMin' => ['integer'], + 'numberMax' => ['integer', 'double'], + 'numberMin' => ['integer', 'double'], 'validationType' => ['string'], 'validationRegex' => ['string'], ]; + public const EXTRA_SETTINGS_LONG = [ + 'lengthMax' => ['integer'], + 'lengthMin' => ['integer'], + ]; + public const EXTRA_SETTINGS_FILE = [ 'allowedFileTypes' => ['array'], 'allowedFileExtensions' => ['array'], diff --git a/lib/ResponseDefinitions.php b/lib/ResponseDefinitions.php index 0db8b7bd4..60b7e6301 100644 --- a/lib/ResponseDefinitions.php +++ b/lib/ResponseDefinitions.php @@ -27,8 +27,12 @@ * dateMax?: string|int, * dateMin?: string|int, * dateRange?: bool, + * lengthMax?: int, + * lengthMin?: int, * maxAllowedFilesCount?: int, * maxFileSize?: int, + * numberMax?: int|float, + * numberMin?: int|float, * optionsHighest?: 2|3|4|5|6|7|8|9|10, * optionsLabelHighest?: string, * optionsLabelLowest?: string, diff --git a/lib/Service/FormsService.php b/lib/Service/FormsService.php index 39c74a8ba..5cf7e1da3 100644 --- a/lib/Service/FormsService.php +++ b/lib/Service/FormsService.php @@ -905,6 +905,7 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType Constants::ANSWER_TYPE_DROPDOWN => Constants::EXTRA_SETTINGS_DROPDOWN, Constants::ANSWER_TYPE_MULTIPLE, Constants::ANSWER_TYPE_MULTIPLEUNIQUE => Constants::EXTRA_SETTINGS_MULTIPLE, Constants::ANSWER_TYPE_SHORT => Constants::EXTRA_SETTINGS_SHORT, + Constants::ANSWER_TYPE_LONG => Constants::EXTRA_SETTINGS_LONG, Constants::ANSWER_TYPE_FILE => Constants::EXTRA_SETTINGS_FILE, Constants::ANSWER_TYPE_DATE => Constants::EXTRA_SETTINGS_DATE, Constants::ANSWER_TYPE_GRID => Constants::EXTRA_SETTINGS_GRID, @@ -927,6 +928,10 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType } } + if (!$this->areAnswerLimitsValid($extraSettings)) { + return false; + } + // Validate extraSettings for specific question types if ($questionType === Constants::ANSWER_TYPE_DATE) { $format = Constants::ANSWER_PHPDATETIME_FORMAT['date']; @@ -1085,6 +1090,54 @@ public function areExtraSettingsValid(array $extraSettings, string $questionType return true; } + /** + * Whether the length and number limits of a text answer are consistent + * + * A length limit is allowed on plain text answers only, a number range only + * on answers validated as number. Lengths are counted in characters from 1 + * up to the stored answer length, and a minimum must not exceed its maximum. + * + * @param array $extraSettings the extra settings of the question + * @return bool true if the limits are valid or not set + */ + private function areAnswerLimitsValid(array $extraSettings): bool { + $validationType = $extraSettings['validationType'] ?? null; + $lengthMin = $extraSettings['lengthMin'] ?? null; + $lengthMax = $extraSettings['lengthMax'] ?? null; + $numberMin = $extraSettings['numberMin'] ?? null; + $numberMax = $extraSettings['numberMax'] ?? null; + + if ($lengthMin !== null || $lengthMax !== null) { + if ($validationType !== null) { + return false; + } + foreach ([$lengthMin, $lengthMax] as $length) { + if ($length !== null && ($length < 1 || $length > Constants::MAX_STRING_LENGTHS['answerText'])) { + return false; + } + } + if ($lengthMin !== null && $lengthMax !== null && $lengthMin > $lengthMax) { + return false; + } + } + + if ($numberMin !== null || $numberMax !== null) { + if ($validationType !== 'number') { + return false; + } + foreach ([$numberMin, $numberMax] as $number) { + if ($number !== null && !is_finite((float)$number)) { + return false; + } + } + if ($numberMin !== null && $numberMax !== null && $numberMin > $numberMax) { + return false; + } + } + + return true; + } + /** * Get list of forms * diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index b379b9752..b4c27e162 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -769,6 +769,11 @@ public function validateSubmission(array $questions, array $answers, string $for throw new \InvalidArgumentException(sprintf('Invalid input for question "%s".', $question['text'])); } + if (in_array($question['type'], [Constants::ANSWER_TYPE_SHORT, Constants::ANSWER_TYPE_LONG], true) + && $answers[$questionId][0] !== '') { + $this->validateAnswerLimits($question, $answers[$questionId][0]); + } + // Handle ranking questions: answers must be a permutation of all option IDs if ($question['type'] === Constants::ANSWER_TYPE_RANKING) { $optionIds = array_map(intval(...), array_column($question['options'] ?? [], 'id')); @@ -918,6 +923,29 @@ private function validateShortQuestion(array $question, string $data): bool { } } + /** + * Check a text answer against the length limits and the number range of its question + * + * @param array $question the question + * @param string $answer the answer text + * @throws \InvalidArgumentException if the answer is too short, too long or out of range + */ + private function validateAnswerLimits(array $question, string $answer): void { + $lengthMin = $question['extraSettings']['lengthMin'] ?? null; + $lengthMax = $question['extraSettings']['lengthMax'] ?? null; + $length = mb_strlen($answer); + if (($lengthMin !== null && $length < $lengthMin) || ($lengthMax !== null && $length > $lengthMax)) { + throw new \InvalidArgumentException(sprintf('The answer for question "%s" must be from %d to %d characters long.', $question['text'], $lengthMin ?? 1, $lengthMax ?? Constants::MAX_STRING_LENGTHS['answerText'])); + } + + $numberMin = $question['extraSettings']['numberMin'] ?? null; + $numberMax = $question['extraSettings']['numberMax'] ?? null; + if (($numberMin !== null || $numberMax !== null) && is_numeric($answer) + && (($numberMin !== null && +$answer < $numberMin) || ($numberMax !== null && +$answer > $numberMax))) { + throw new \InvalidArgumentException(sprintf('The answer for question "%s" must be a number from %s to %s.', $question['text'], $numberMin ?? '-∞', $numberMax ?? '∞')); + } + } + private function validateGridQuestion(array $question, array $answers): void { $rowIds = $this->normalizeOptionIds($question['options'] ?? [], Constants::OPTION_TYPE_ROW); $columnIds = $this->normalizeOptionIds($question['options'] ?? [], Constants::OPTION_TYPE_COLUMN); diff --git a/openapi.json b/openapi.json index 58430d3e1..b44bf6af6 100644 --- a/openapi.json +++ b/openapi.json @@ -530,6 +530,14 @@ "dateRange": { "type": "boolean" }, + "lengthMax": { + "type": "integer", + "format": "int64" + }, + "lengthMin": { + "type": "integer", + "format": "int64" + }, "maxAllowedFilesCount": { "type": "integer", "format": "int64" @@ -538,6 +546,30 @@ "type": "integer", "format": "int64" }, + "numberMax": { + "anyOf": [ + { + "type": "integer", + "format": "int64" + }, + { + "type": "number", + "format": "double" + } + ] + }, + "numberMin": { + "anyOf": [ + { + "type": "integer", + "format": "int64" + }, + { + "type": "number", + "format": "double" + } + ] + }, "optionsHighest": { "type": "integer", "format": "int64", diff --git a/src/components/Questions/QuestionLong.vue b/src/components/Questions/QuestionLong.vue index 91bebca17..cda4fe4dd 100644 --- a/src/components/Questions/QuestionLong.vue +++ b/src/components/Questions/QuestionLong.vue @@ -10,14 +10,54 @@ :titlePlaceholder="answerType.titlePlaceholder" :warningInvalid="answerType.warningInvalid" :errorMessage="errorMessage" + :infoMessage="hasError ? undefined : (limitHint ?? undefined)" v-on="commonListeners"> +