From 55f438d8d9a30dab45a2be30960e6d475a2baa14 Mon Sep 17 00:00:00 2001 From: orshih6 <34618353+orshih6@users.noreply.github.com> Date: Sun, 25 Oct 2026 00:46:12 +0000 Subject: [PATCH] fix: skip short answer validation for empty optional answers --- lib/Service/SubmissionService.php | 6 +++-- tests/Unit/Service/SubmissionServiceTest.php | 24 ++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/Service/SubmissionService.php b/lib/Service/SubmissionService.php index 278fb2828..95024563a 100644 --- a/lib/Service/SubmissionService.php +++ b/lib/Service/SubmissionService.php @@ -699,8 +699,10 @@ public function validateSubmission(array $questions, array $answers, string $for } } - // Handle custom validation of short answers - if ($question['type'] === Constants::ANSWER_TYPE_SHORT && !$this->validateShortQuestion($question, $answers[$questionId][0])) { + // Handle custom validation of short answers, empty answers are not stored so skip validation for them + if ($question['type'] === Constants::ANSWER_TYPE_SHORT + && $answers[$questionId][0] !== '' + && !$this->validateShortQuestion($question, $answers[$questionId][0])) { throw new \InvalidArgumentException(sprintf('Invalid input for question "%s".', $question['text'])); } diff --git a/tests/Unit/Service/SubmissionServiceTest.php b/tests/Unit/Service/SubmissionServiceTest.php index e3d19a7e4..551b1c6d3 100644 --- a/tests/Unit/Service/SubmissionServiceTest.php +++ b/tests/Unit/Service/SubmissionServiceTest.php @@ -969,6 +969,30 @@ public static function dataValidateSubmission() { // Expected Result 'Invalid input for question "q1".', ], + 'empty-short-email-not-required' => [ + // Questions + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false, 'extraSettings' => ['validationType' => 'email']] + ], + // Answers + [ + '1' => [''] + ], + // Expected Result + null, + ], + 'empty-short-email-required' => [ + // Questions + [ + ['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => true, 'extraSettings' => ['validationType' => 'email']] + ], + // Answers + [ + '1' => [''] + ], + // Expected Result + 'Question "q1" is required.', + ], 'invalid-short-regex-not-matching' => [ // Questions [