зеркало из https://github.com/nextcloud/forms.git
Validate date/time answers
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
This commit is contained in:
Родитель
5fe7009109
Коммит
5352a2dc29
|
@ -40,4 +40,10 @@ class Constants {
|
|||
|
||||
// AnswerTypes, that need/have predefined Options
|
||||
public const ANSWER_PREDEFINED = [self::ANSWER_TYPE_MULTIPLE, self::ANSWER_TYPE_MULTIPLEUNIQUE, self::ANSWER_TYPE_DROPDOWN];
|
||||
|
||||
// AnswerTypes for date/time questions
|
||||
public const ANSWER_DATETIME = [self::ANSWER_TYPE_DATE, self::ANSWER_TYPE_DATETIME, self::ANSWER_TYPE_TIME];
|
||||
|
||||
// Formats for AnswerTypes date/datetime/time
|
||||
public const ANSWER_PHPDATETIME_FORMAT = [self::ANSWER_TYPE_DATE => 'Y-m-d', self::ANSWER_TYPE_DATETIME => 'Y-m-d H:i', self::ANSWER_TYPE_TIME => 'H:i'];
|
||||
}
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
namespace OCA\Forms\Service;
|
||||
|
||||
use DateTimeZone;
|
||||
use DateTime;
|
||||
|
||||
use OCA\Forms\Constants;
|
||||
use OCA\Forms\Db\FormMapper;
|
||||
|
@ -261,12 +262,20 @@ class SubmissionService {
|
|||
}
|
||||
|
||||
// Perform further checks only for answered questions
|
||||
// TODO Check if date questions have valid answers
|
||||
if ($questionAnswered) {
|
||||
// Check if non multiple questions have not more than one answer
|
||||
if ($question['type'] !== Constants::ANSWER_TYPE_MULTIPLE && count($answers[$questionId]) > 1) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/*
|
||||
* Check if date questions have valid answers
|
||||
* $answers[$questionId][0] -> date/time questions can only have one answer
|
||||
*/
|
||||
if (in_array($question['type'], Constants::ANSWER_DATETIME) &&
|
||||
!$this->validateDateTime($answers[$questionId][0], Constants::ANSWER_PHPDATETIME_FORMAT[$question['type']])) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if all answers are within the possible options
|
||||
if (in_array($question['type'], Constants::ANSWER_PREDEFINED)) {
|
||||
|
@ -291,4 +300,15 @@ class SubmissionService {
|
|||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate correct date/time formats
|
||||
* @param string $dateStr String with date from answer
|
||||
* @param string $format String with the format to validate
|
||||
* @return boolean If the submitted date/time is valid
|
||||
*/
|
||||
private function validateDateTime(string $dateStr, string $format) {
|
||||
$d = DateTime::createFromFormat($format, $dateStr);
|
||||
return $d && $d->format($format) === $dateStr;
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче