Skip to content

Commit 3c8d2d0

Browse files
committed
enh: improve error messages for required questions.
Signed-off-by: Christian Hartmann <chris-hartmann@gmx.de>
1 parent a138698 commit 3c8d2d0

4 files changed

Lines changed: 32 additions & 69 deletions

File tree

src/components/Questions/QuestionDropdown.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
:warningInvalid="answerType.warningInvalid"
1111
:contentValid="contentValid"
1212
:shiftDragHandle="shiftDragHandle"
13+
:errorMessage="errorMessage"
1314
v-on="commonListeners">
1415
<template #actions>
1516
<NcActionCheckbox
@@ -184,6 +185,16 @@ export default {
184185
},
185186
186187
methods: {
188+
async validate() {
189+
if (this.isRequired && this.areNoneChecked) {
190+
this.errorMessage = t('forms', 'You must answer this question')
191+
return false
192+
}
193+
194+
this.errorMessage = null
195+
return true
196+
},
197+
187198
onDragStart() {
188199
this.isDragging = true
189200
},

src/components/Questions/QuestionGrid.vue

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,11 @@ export default {
264264
265265
methods: {
266266
async validate() {
267+
if (this.isRequired && this.areNoneChecked) {
268+
this.errorMessage = t('forms', 'You must answer this question')
269+
return false
270+
}
271+
267272
if (!this.isUnique) {
268273
// Validate limits
269274
const max = this.extraSettings.optionsLimitMax ?? 0
@@ -315,30 +320,6 @@ export default {
315320
316321
this.$emit('update:values', values)
317322
},
318-
319-
/**
320-
* Is the provided answer required ?
321-
* This is needed for checkboxes as html5
322-
* doesn't allow to require at least ONE checked.
323-
* So we require the one that are checked or all
324-
* if none are checked yet.
325-
*
326-
* @return {boolean}
327-
*/
328-
checkRequired() {
329-
// false, if question not required
330-
if (!this.isRequired) {
331-
return false
332-
}
333-
334-
// true for Radiobuttons
335-
if (this.isUnique) {
336-
return true
337-
}
338-
339-
// For checkboxes, only required if no other is checked
340-
return this.areNoneChecked
341-
},
342323
},
343324
}
344325
</script>

src/components/Questions/QuestionLinearScale.vue

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
v-bind="questionProps"
99
:titlePlaceholder="answerType.titlePlaceholder"
1010
:warningInvalid="answerType.warningInvalid"
11+
:errorMessage="errorMessage"
1112
v-on="commonListeners">
1213
<template #actions>
1314
<NcActionInput
@@ -85,7 +86,6 @@
8586
:value="option.toString()"
8687
:name="`${id}-answer`"
8788
type="radio"
88-
:required="checkRequired(option)"
8989
@update:modelValue="onChange"
9090
@keydown.enter.exact.prevent="onKeydownEnter" />
9191
</div>
@@ -203,6 +203,16 @@ export default {
203203
},
204204
205205
methods: {
206+
async validate() {
207+
if (this.isRequired && this.values.length === 0) {
208+
this.errorMessage = t('forms', 'You must answer this question')
209+
return false
210+
}
211+
212+
this.errorMessage = null
213+
return true
214+
},
215+
206216
onChange(option) {
207217
this.$emit('update:values', [option])
208218
},
@@ -231,24 +241,6 @@ export default {
231241
})
232242
},
233243
234-
/**
235-
* Is the provided answer required ?
236-
* This is needed for checkboxes as html5
237-
* doesn't allow to require at least ONE checked.
238-
* So we require the one that are checked or all
239-
* if none are checked yet.
240-
*
241-
* @return {boolean}
242-
*/
243-
checkRequired() {
244-
// false, if question not required
245-
if (!this.isRequired) {
246-
return false
247-
}
248-
249-
return true
250-
},
251-
252244
/**
253245
* Resizes the given label to fit within the specified constraints.
254246
*

src/components/Questions/QuestionMultiple.vue

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@
8484
:value="answer.id.toString()"
8585
:name="`${id}-answer`"
8686
:type="isUnique ? 'radio' : 'checkbox'"
87-
:required="checkRequired(answer.id)"
8887
@update:modelValue="onChange"
8988
@keydown.enter.exact.prevent="onKeydownEnter">
9089
{{ answer.text }}
@@ -97,7 +96,6 @@
9796
:value="otherAnswer ?? QUESTION_EXTRASETTINGS_OTHER_PREFIX"
9897
:name="`${id}-answer`"
9998
:type="isUnique ? 'radio' : 'checkbox'"
100-
:required="checkRequired('other-answer')"
10199
class="question__label"
102100
@update:modelValue="onChangeOther"
103101
@keydown.enter.exact.prevent="onKeydownEnter">
@@ -361,6 +359,11 @@ export default {
361359
362360
methods: {
363361
async validate() {
362+
if (this.isRequired && this.areNoneChecked) {
363+
this.errorMessage = t('forms', 'You must answer this question')
364+
return false
365+
}
366+
364367
if (!this.isUnique) {
365368
// Validate limits
366369
const max = this.extraSettings.optionsLimitMax ?? 0
@@ -518,30 +521,6 @@ export default {
518521
}
519522
},
520523
521-
/**
522-
* Is the provided answer required ?
523-
* This is needed for checkboxes as html5
524-
* doesn't allow to require at least ONE checked.
525-
* So we require the one that are checked or all
526-
* if none are checked yet.
527-
*
528-
* @return {boolean}
529-
*/
530-
checkRequired() {
531-
// false, if question not required
532-
if (!this.isRequired) {
533-
return false
534-
}
535-
536-
// true for Radiobuttons
537-
if (this.isUnique) {
538-
return true
539-
}
540-
541-
// For checkboxes, only required if no other is checked
542-
return this.areNoneChecked
543-
},
544-
545524
/**
546525
* Update status extra setting allowOtherAnswer and save on DB
547526
*

0 commit comments

Comments
 (0)