Merge pull request #11158 from AlexSCorey/11137-FixSurveyIntegerValidation

Properly validates Integer Survey Question with min value of 0
This commit is contained in:
Sarah Akus
2021-10-01 12:36:19 -04:00
committed by GitHub
2 changed files with 7 additions and 4 deletions

View File

@@ -72,7 +72,7 @@ function getInitialValues(launchConfig, surveyConfig, resource) {
? question.default.split('\n') ? question.default.split('\n')
: []; : [];
} else { } else {
values[`survey_${question.variable}`] = question.default || ''; values[`survey_${question.variable}`] = question.default ?? '';
} }
if (resource?.extra_data) { if (resource?.extra_data) {
Object.entries(resource.extra_data).forEach(([key, value]) => { Object.entries(resource.extra_data).forEach(([key, value]) => {
@@ -129,11 +129,14 @@ function checkForError(launchConfig, surveyConfig, values) {
} }
} }
if (isNumeric && (value || value === 0)) { if (isNumeric && (value || value === 0)) {
if (value < question.min || value > question.max) { if (
(value < question.min || value > question.max) &&
question.required
) {
hasError = true; hasError = true;
} }
} }
if (question.required && (!value || value.length === 0)) { if (question.required && (!value || value.length === 0) && !isNumeric) {
hasError = true; hasError = true;
} }
}); });

View File

@@ -114,7 +114,7 @@ function SurveyQuestionForm({
variable: question?.variable || '', variable: question?.variable || '',
min: question?.min || 0, min: question?.min || 0,
max: question?.max || 1024, max: question?.max || 1024,
default: question?.default || '', default: question?.default ?? '',
choices: question?.choices || '', choices: question?.choices || '',
formattedChoices: [{ choice: '', isDefault: false, id: 0 }], formattedChoices: [{ choice: '', isDefault: false, id: 0 }],
new_question: !question, new_question: !question,