Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/Service/FormsService.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,11 @@ public function getQuestions(int $formId): array {
}
}

// Set `isRequired` if minimum options limit is set
if ($question['type'] === Constants::ANSWER_TYPE_MULTIPLE && $question['extraSettings']['optionsLimitMin'] > 0) {
$question['isRequired'] = true;
}

$questionList[] = $question;
}
} catch (DoesNotExistException $e) {
Expand Down
16 changes: 16 additions & 0 deletions src/components/Questions/Question.vue
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
v-html="computedDescription" />
<!-- eslint-enable vue/no-v-html -->
</div>
<NcNoteCard v-if="hasInfo" :id="infoId" type="info">
{{ infoMessage }}
</NcNoteCard>
<NcNoteCard v-if="hasError" :id="errorId" type="error">
{{ errorMessage }}
</NcNoteCard>
Expand Down Expand Up @@ -248,7 +251,7 @@

contentValid: {
type: Boolean,
default: true,

Check warning on line 254 in src/components/Questions/Question.vue

View workflow job for this annotation

GitHub Actions / NPM lint

Boolean prop should only be defaulted to false
},

warningInvalid: {
Expand All @@ -270,6 +273,11 @@
type: String,
default: null,
},

infoMessage: {
type: String,
default: null,
},
},

emits: [
Expand Down Expand Up @@ -329,9 +337,17 @@
return !!this.errorMessage
},

hasInfo() {
return !!this.infoMessage
},

errorId() {
return `q${this.index}_error`
},

infoId() {
return `q${this.index}_info`
},
},

// Ensure description is sized correctly on initial render
Expand Down
82 changes: 80 additions & 2 deletions src/components/Questions/QuestionMultiple.vue
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
:contentValid="contentValid"
:shiftDragHandle="shiftDragHandle"
:errorMessage="errorMessage"
:infoMessage="infoMessage"
v-on="commonListeners">
<template #actions>
<NcActionCheckbox
Expand Down Expand Up @@ -135,7 +136,7 @@
<AnswerInput
v-for="(answer, index) in choices"
:key="answer.local ? 'option-local' : answer.id"
ref="input"

Check warning on line 139 in src/components/Questions/QuestionMultiple.vue

View workflow job for this annotation

GitHub Actions / NPM lint

'input' is defined as ref, but never used
:answer="answer"
:formId="formId"
:index="index"
Expand Down Expand Up @@ -217,7 +218,7 @@
},

mixins: [QuestionMixin, QuestionMultipleMixin],
emits: ['update:values'],
emits: ['update:values', 'update:isRequired'],

data() {
return {
Expand Down Expand Up @@ -282,6 +283,54 @@
this.updateOptionsOrder(value, OptionType.Choice)
},
},

availableOptions() {
return (
this.choices.filter(({ text }) => text.trim() !== '').length
+ (this.allowOtherAnswer ? 1 : 0)
)
},

infoMessage() {
const min = this.extraSettings?.optionsLimitMin ?? 0
const max = this.extraSettings?.optionsLimitMax ?? 0

if (!min && !max) {
return null
}

if (min && max) {
if (min === max) {
return n(
'forms',
'Choose exactly one option',
'Choose exactly %n options',
min,
)
}

return t('forms', 'Choose between {min} and {max} options', {
min,
max,
})
}

if (min) {
return n(
'forms',
'Choose at least one option',
'Choose at least %n options',
min,
)
}

return n(
'forms',
'Choose at most one option',
'Choose at most %n options',
max,
)
},
},

watch: {
Expand All @@ -306,7 +355,7 @@
this.errorMessage = n(
'forms',
'You must choose at most one option',
'You must choose a maximum of %n options',
'You must choose at most %n options',
max,
)
return false
Expand Down Expand Up @@ -376,6 +425,19 @@
// For unique (radio) options we cannot set limits, also if null is passed then we need to remove the limit
this.onExtraSettingsChange({ optionsLimitMax: undefined })
} else if (max) {
if (max > this.availableOptions) {
showError(
t(
'forms',
'Upper options limit must not exceed the number of available options',
),
)
this.onExtraSettingsChange({
optionsLimitMax: this.availableOptions || undefined,
})
return
}

if ((this.extraSettings.optionsLimitMin ?? 0) > max) {
showError(
t(
Expand All @@ -400,6 +462,19 @@
if (this.isUnique || min === null) {
this.onExtraSettingsChange({ optionsLimitMin: undefined })
} else if (min) {
if (min > this.availableOptions - 1) {
showError(
t(
'forms',
'Lower options limit must be smaller than the number of available options',
),
)
this.onExtraSettingsChange({
optionsLimitMin: this.availableOptions - 1 || undefined,
})
return
}

if (
this.extraSettings.optionsLimitMax
&& min > this.extraSettings.optionsLimitMax
Expand All @@ -413,6 +488,9 @@
return
}
this.onExtraSettingsChange({ optionsLimitMin: min })
if (min > 0) {
this.$emit('update:isRequired', true)
}
}
},

Expand Down
10 changes: 9 additions & 1 deletion src/mixins/QuestionMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,16 @@ export default {
return !!this.errorMessage
},

hasInfo() {
return !!this.infoMessage
},

errorId() {
return 'q' + this.index + '_error'
return `q${this.index}_error`
},

infoId() {
return `q${this.index}_info`
},

/**
Expand Down
Loading