@@ -374,17 +374,21 @@ public function validateSubmission(array $questions, array $answers, string $for
374
374
} elseif ($ maxOptions > 0 && $ answersCount > $ maxOptions ) {
375
375
throw new \InvalidArgumentException (sprintf ('Question "%s" requires at most %d answers. ' , $ question ['text ' ], $ maxOptions ));
376
376
}
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 ' ]))) {
378
381
// Check if non-multiple questions have not more than one answer
379
382
throw new \InvalidArgumentException (sprintf ('Question "%s" can only have one answer. ' , $ question ['text ' ]));
380
383
}
381
384
382
385
/*
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
385
389
*/
386
390
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 );
388
392
}
389
393
390
394
// Check if all answers are within the possible options
@@ -433,22 +437,31 @@ public function validateSubmission(array $questions, array $answers, string $for
433
437
434
438
/**
435
439
* Validate correct date/time formats
436
- * @param string $dateStr String with date from answer
440
+ * @param array $answers Array with date from answer
437
441
* @param string $format String with the format to validate
438
442
* @param string|null $text String with the title of the question
439
443
* @param array|null $extraSettings Array with extra settings for validation
440
444
*/
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 ;
446
447
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
+ }
452
465
}
453
466
}
454
467
}
0 commit comments