@@ -374,17 +374,21 @@ public function validateSubmission(array $questions, array $answers, string $for
374374 } elseif ($ maxOptions > 0 && $ answersCount > $ maxOptions ) {
375375 throw new \InvalidArgumentException (sprintf ('Question "%s" requires at most %d answers. ' , $ question ['text ' ], $ maxOptions ));
376376 }
377- } elseif ($ answersCount > 1 && $ question ['type ' ] !== Constants::ANSWER_TYPE_FILE ) {
377+ } elseif ($ answersCount != 2 && $ question ['type ' ] !== Constants::ANSWER_TYPE_DATE && isset ($ question ['extraSettings ' ]['dateRange ' ])) {
378+ // Check if date range questions have exactly two answers
379+ throw new \InvalidArgumentException (sprintf ('Question "%s" can only have two answers. ' , $ question ['text ' ]));
380+ } elseif ($ answersCount > 1 && $ question ['type ' ] !== Constants::ANSWER_TYPE_FILE && !($ question ['type ' ] === Constants::ANSWER_TYPE_DATE && isset ($ question ['extraSettings ' ]['dateRange ' ]))) {
378381 // Check if non-multiple questions have not more than one answer
379382 throw new \InvalidArgumentException (sprintf ('Question "%s" can only have one answer. ' , $ question ['text ' ]));
380383 }
381384
382385 /*
383- * Check if date questions have valid answers
384- * $answers[$questionId][0] -> date/time questions can only have one answer
386+ * Validate answers for date/time questions
387+ * If a date range is specified, validate all answers in the range
388+ * Otherwise, validate the single answer for the date/time question
385389 */
386390 if (in_array ($ question ['type ' ], Constants::ANSWER_TYPES_DATETIME )) {
387- $ this ->validateDateTime ($ answers [$ questionId ][ 0 ] , Constants::ANSWER_PHPDATETIME_FORMAT [$ question ['type ' ]], $ question ['text ' ] ?? null , $ question ['extraSettings ' ] ?? null );
391+ $ this ->validateDateTime ($ answers [$ questionId ], Constants::ANSWER_PHPDATETIME_FORMAT [$ question ['type ' ]], $ question ['text ' ] ?? null , $ question ['extraSettings ' ] ?? null );
388392 }
389393
390394 // Check if all answers are within the possible options
@@ -433,22 +437,31 @@ public function validateSubmission(array $questions, array $answers, string $for
433437
434438 /**
435439 * Validate correct date/time formats
436- * @param string $dateStr String with date from answer
440+ * @param array $answers Array with date from answer
437441 * @param string $format String with the format to validate
438442 * @param string|null $text String with the title of the question
439443 * @param array|null $extraSettings Array with extra settings for validation
440444 */
441- private function validateDateTime (string $ dateStr , string $ format , ?string $ text = null , ?array $ extraSettings = null ): void {
442- $ d = DateTime::createFromFormat ($ format , $ dateStr );
443- if (!$ d || $ d ->format ($ format ) !== $ dateStr ) {
444- throw new \InvalidArgumentException (sprintf ('Invalid date/time format for question "%s". ' , $ text ));
445- }
445+ private function validateDateTime (array $ answers , string $ format , ?string $ text = null , ?array $ extraSettings = null ): void {
446+ $ previousDate = null ;
446447
447- if ($ extraSettings ) {
448- if ((isset ($ extraSettings ['dateMin ' ]) && $ d < (new DateTime ())->setTimestamp ($ extraSettings ['dateMin ' ])) ||
449- (isset ($ extraSettings ['dateMax ' ]) && $ d > (new DateTime ())->setTimestamp ($ extraSettings ['dateMax ' ]))
450- ) {
451- throw new \InvalidArgumentException (sprintf ('Date is not in the allowed range for question "%s". ' , $ text ));
448+ foreach ($ answers as $ dateStr ) {
449+ $ d = DateTime::createFromFormat ($ format , $ dateStr );
450+ if (!$ d || $ d ->format ($ format ) !== $ dateStr ) {
451+ throw new \InvalidArgumentException (sprintf ('Invalid date/time format for question "%s". ' , $ text ));
452+ }
453+
454+ if ($ previousDate !== null && $ d < $ previousDate ) {
455+ throw new \InvalidArgumentException (sprintf ('Dates for question "%s" must be in ascending order. ' , $ text ));
456+ }
457+ $ previousDate = $ d ;
458+
459+ if ($ extraSettings ) {
460+ if ((isset ($ extraSettings ['dateMin ' ]) && $ d < (new DateTime ())->setTimestamp ($ extraSettings ['dateMin ' ])) ||
461+ (isset ($ extraSettings ['dateMax ' ]) && $ d > (new DateTime ())->setTimestamp ($ extraSettings ['dateMax ' ]))
462+ ) {
463+ throw new \InvalidArgumentException (sprintf ('Date is not in the allowed range for question "%s". ' , $ text ));
464+ }
452465 }
453466 }
454467 }
0 commit comments