зеркало из https://github.com/nextcloud/forms.git
Validate submission
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
This commit is contained in:
Родитель
f5faf3f288
Коммит
ced76feb9d
|
@ -973,6 +973,11 @@ class ApiController extends OCSController {
|
|||
throw new OCSForbiddenException('Already submitted');
|
||||
}
|
||||
|
||||
// Is the submission valid
|
||||
if (!$this->submissionService->validateSubmission($questions, $answers)) {
|
||||
throw new OCSBadRequestException('At least one submitted answer is not valid');
|
||||
}
|
||||
|
||||
// Create Submission
|
||||
$submission = new Submission();
|
||||
$submission->setFormId($formId);
|
||||
|
|
|
@ -26,6 +26,7 @@ namespace OCA\Forms\Service;
|
|||
|
||||
use DateTimeZone;
|
||||
|
||||
use OCA\Forms\Constants;
|
||||
use OCA\Forms\Db\FormMapper;
|
||||
use OCA\Forms\Db\QuestionMapper;
|
||||
use OCA\Forms\Db\SubmissionMapper;
|
||||
|
@ -240,4 +241,54 @@ class SubmissionService {
|
|||
|
||||
return $csv->getContent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate all answers against the questions
|
||||
* @param array $questions Array of the questions of the form
|
||||
* @param array $answers Array of the submitted answers
|
||||
* @return boolean If the submission is valid
|
||||
*/
|
||||
public function validateSubmission(array $questions, array $answers): bool {
|
||||
|
||||
// Check by questions
|
||||
foreach ($questions as $question) {
|
||||
$questionId = $question['id'];
|
||||
$questionAnswered = array_key_exists($questionId, $answers);
|
||||
|
||||
// Check if all required questions have an answer
|
||||
if ($question['isRequired'] && (!$questionAnswered || !array_filter($answers[$questionId], 'strlen'))) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// 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 all answers are within the possible options
|
||||
if (in_array($question['type'], Constants::ANSWER_PREDEFINED)) {
|
||||
foreach ($answers[$questionId] as $answer) {
|
||||
// Search corresponding option, return false if non-existent
|
||||
if (array_search($answer, array_column($question['options'], 'id')) === false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Check for excess answers
|
||||
foreach ($answers as $id => $answerArray) {
|
||||
// Search corresponding question, return false if not found
|
||||
$questionIndex = array_search($id, array_column($questions, 'id'));
|
||||
if ($questionIndex === false) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче