Add checkbox validation and error messaging for required options

This commit is contained in:
HoshinoKoji 2025-03-05 15:02:34 +08:00
parent e740c4bb1a
commit 0cad1e7787
2 changed files with 12 additions and 0 deletions

View File

@ -127,6 +127,16 @@ export default {
type: 'error', type: 'error',
}); });
return; return;
} else if (item.type === 'checkbox' && item.required) {
this.itemStatus.answer = this.itemStatus.answer === undefined ? [] : this.itemStatus.answer;
const { minOpts, maxOpts } = Object.assign({ minOpts: 1, maxOpts: item.optTexts.length }, item);
if (this.itemStatus.answer.length < minOpts || this.itemStatus.answer.length > maxOpts) {
ElMessage({
message: sprintf(lang[this.settings.lang].checkboxOutOfRange, minOpts, maxOpts),
type: 'error',
});
return;
}
} }
item.answer = this.itemStatus.answer; item.answer = this.itemStatus.answer;

View File

@ -6,6 +6,7 @@ export default {
'loading': 'Loading...', 'loading': 'Loading...',
'input': 'Please input', 'input': 'Please input',
'required': 'This field is required', 'required': 'This field is required',
'checkboxOutOfRange': 'Please select at least %d and at most %d option(s)',
}, },
'zh-CN': { 'zh-CN': {
'submit': '提交', 'submit': '提交',
@ -14,5 +15,6 @@ export default {
'loading': '加载中...', 'loading': '加载中...',
'input': '请输入', 'input': '请输入',
'required': '此字段不能为空', 'required': '此字段不能为空',
'checkboxOutOfRange': '请选择 %d 到 %d 个选项',
}, },
} }