Skip to content
Snippets Groups Projects
Commit 9f05f299 authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

Also perform survey validation when editing and saving the survey

parent b8124475
No related branches found
No related tags found
1 merge request!133Refactor publishers and minor changes to the frontend
import React, { useCallback } from "react";
import { Question, FunctionFactory } from "survey-core";
import { Question } from "survey-core";
import { Survey } from "survey-react-ui";
import { VerificationStatus } from './Schema';
......@@ -16,19 +16,6 @@ function customDescriptionCallback(_, options) {
}
}
function validateWebsiteUrl(params) {
const value = params[0];
if (value === undefined || value == null || value == '') {
return true;
}
try {
const url = new URL(value);
return !!url
} catch (err) {
return false;
}
}
function hideCheckboxLabels(_, options) {
if (options.question.hideCheckboxLabels) {
const classes = options.cssClasses;
......@@ -123,10 +110,6 @@ function SurveyComponent({ surveyModel }) {
}
}, [surveyModel])
if (!FunctionFactory.Instance.hasFunction("validateWebsiteUrl")) {
FunctionFactory.Instance.register("validateWebsiteUrl", validateWebsiteUrl);
}
if (!surveyModel.css.question.title.includes("sv-header-flex")) {
surveyModel.css.question.title = "sv-title sv-question__title sv-header-flex";
surveyModel.css.question.titleOnError = "sv-question__title--error sv-error-color-fix";
......
......@@ -10,6 +10,23 @@ import Prompt from "./Prompt";
import "survey-core/modern.min.css";
import './survey.scss';
import useMatomo from "../matomo/UseMatomo";
import { FunctionFactory } from "survey-core";
function validateWebsiteUrl(params) {
const value = params[0];
const nonEmpty = params[1] || false;
if (!nonEmpty && (value === undefined || value == null)) {
return true;
}
try {
const url = new URL(value);
return !!url
} catch (err) {
return false;
}
}
Serializer.addProperty("itemvalue", "customDescription:text");
Serializer.addProperty("question", "hideCheckboxLabels:boolean");
......@@ -20,6 +37,10 @@ function SurveyContainerComponent({ loadFrom }) {
const { year, nren } = useParams(); // nren stays empty for inspect and try
const [error, setError] = useState<string>('loading survey...');
if (!FunctionFactory.Instance.hasFunction("validateWebsiteUrl")) {
FunctionFactory.Instance.register("validateWebsiteUrl", validateWebsiteUrl);
}
const { trackPageView } = useMatomo();
const beforeUnloadListener = useCallback((event) => {
......@@ -114,7 +135,7 @@ function SurveyContainerComponent({ loadFrom }) {
}
}
const validateWithAnswerVerification = (validatorFunction) => {
const validateWithAnswerVerification = (validatorFunction, validateStatus = true) => {
let firstValidationError = '';
const verificationValidator = (survey, options) => {
const status = survey.verificationStatus.get(options.name);
......@@ -125,9 +146,9 @@ function SurveyContainerComponent({ loadFrom }) {
options.error = 'Please verify that last years data is correct by editing the answer or pressing the "No change from previous year" button!';
}
};
surveyModel.onValidateQuestion.add(verificationValidator);
if (validateStatus) surveyModel.onValidateQuestion.add(verificationValidator);
const validSurvey = validatorFunction();
surveyModel.onValidateQuestion.remove(verificationValidator);
if (validateStatus) surveyModel.onValidateQuestion.remove(verificationValidator);
if (!validSurvey) {
toast("Validation failed!");
}
......@@ -136,6 +157,11 @@ function SurveyContainerComponent({ loadFrom }) {
const surveyActions = {
'save': async () => {
const allFieldsValid = validateWithAnswerVerification(surveyModel.validate.bind(surveyModel, true, true), false);
if (!allFieldsValid) {
toast("Please correct the invalid fields before saving!");
return;
}
const errorMessage = await saveSurveyData(surveyModel, "editing");
if (errorMessage) {
toast("Failed saving survey: " + errorMessage);
......@@ -157,6 +183,11 @@ function SurveyContainerComponent({ loadFrom }) {
}
},
'saveAndStopEdit': async () => {
const allFieldsValid = validateWithAnswerVerification(surveyModel.validate.bind(surveyModel, true, true), false);
if (!allFieldsValid) {
toast("Please correct the invalid fields before saving.");
return;
}
const errorMessage = await saveSurveyData(surveyModel, "readonly");
if (errorMessage) {
toast("Failed saving survey: " + errorMessage);
......@@ -184,6 +215,13 @@ function SurveyContainerComponent({ loadFrom }) {
surveyModel.lockedBy = json['locked_by']
surveyModel.lockUUID = json['lock_uuid'];
surveyModel.status = json['status'];
// Validate when we start editing to ensure invalid fields are corrected by the user
const allFieldsValid = validateWithAnswerVerification(surveyModel.validate.bind(surveyModel, true, true), false);
if (!allFieldsValid) {
toast("Some fields are invalid, please correct them.");
return;
}
},
'releaseLock': async () => {
const response = await fetch('/api/response/unlock/' + year + '/' + nren, { method: 'POST' });
......
This diff is collapsed.
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment