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
27 changes: 26 additions & 1 deletion lib/Service/SubmissionService.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,11 @@ public function validateSubmission(array $questions, array $answers, string $for
// Check by questions
foreach ($questions as $question) {
$questionId = $question['id'];
$questionAnswered = array_key_exists($questionId, $answers);
$questionAnswered = array_key_exists($questionId, $answers) && $answers[$questionId] !== [];

if ($questionAnswered && !self::isListOfValues($question['type'], $answers[$questionId])) {
throw new \InvalidArgumentException(sprintf('Invalid answer for question "%s".', $question['text']));
}

// Check if all required questions have an answer
if ($question['isRequired']
Expand Down Expand Up @@ -921,6 +925,27 @@ private function validateGridQuestion(array $question, array $answers): void {
}
}

/**
* Whether the answer is a list of values: texts or numbers, uploaded files for file questions, or an object with the values by row for grid questions
*
* @param string $questionType the question type
* @param mixed $answer the answer
*/
private static function isListOfValues(string $questionType, mixed $answer): bool {
if ($questionType === Constants::ANSWER_TYPE_GRID) {
return is_array($answer);
}
if (!is_array($answer) || !array_is_list($answer)) {
return false;
}
foreach ($answer as $value) {
if ($questionType === Constants::ANSWER_TYPE_FILE ? !is_array($value) : !is_scalar($value)) {
return false;
}
}
return true;
}

private function normalizeNumericOptionId(mixed $value): ?int {
if (is_int($value)) {
return $value;
Expand Down
43 changes: 42 additions & 1 deletion tests/Unit/Service/SubmissionServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -997,6 +997,47 @@ public static function dataValidateSubmission() {
// Expected Result
null,
],
'empty-answers-to-optional-questions' => [
[
['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false],
['id' => 2, 'type' => 'multiple', 'text' => 'q2', 'isRequired' => false, 'extraSettings' => ['optionsLimitMin' => 2], 'options' => [
['id' => 3],
['id' => 5],
]],
],
['1' => [], '2' => []],
null,
],
'empty-answer-to-required-question' => [
[
['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => true],
],
['1' => []],
'Question "q1" is required.',
],
'answer-that-is-no-list' => [
[
['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false],
],
['1' => ['first' => 'text']],
'Invalid answer for question "q1".',
],
'answer-with-a-value-that-is-no-text' => [
[
['id' => 1, 'type' => 'short', 'text' => 'q1', 'isRequired' => false],
],
['1' => [null]],
'Invalid answer for question "q1".',
],
'answer-that-is-no-array' => [
[
['id' => 1, 'type' => 'multiple_unique', 'text' => 'q1', 'isRequired' => false, 'options' => [
['id' => 3],
]],
],
['1' => '3'],
'Invalid answer for question "q1".',
],
'invalid-short-phone' => [
// Questions
[
Expand Down Expand Up @@ -1265,7 +1306,7 @@ public static function dataValidateSubmission() {
[
'1' => [['3']]
],
'The answer for question "q1" must be an integer between 0 and 5.',
'Invalid answer for question "q1".',
],
'invalid-color-question' => [
// Questions
Expand Down
Loading