From 0cad1e778761168d2b653f68866a1a2620749b2e Mon Sep 17 00:00:00 2001 From: HoshinoKoji Date: Wed, 5 Mar 2025 15:02:34 +0800 Subject: [PATCH] Add checkbox validation and error messaging for required options --- src/App.vue | 10 ++++++++++ src/lang.js | 2 ++ 2 files changed, 12 insertions(+) diff --git a/src/App.vue b/src/App.vue index df4d1be..3d9b97d 100644 --- a/src/App.vue +++ b/src/App.vue @@ -127,6 +127,16 @@ export default { type: 'error', }); 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; diff --git a/src/lang.js b/src/lang.js index 3bf127a..8fec5ee 100644 --- a/src/lang.js +++ b/src/lang.js @@ -6,6 +6,7 @@ export default { 'loading': 'Loading...', 'input': 'Please input', 'required': 'This field is required', + 'checkboxOutOfRange': 'Please select at least %d and at most %d option(s)', }, 'zh-CN': { 'submit': '提交', @@ -14,5 +15,6 @@ export default { 'loading': '加载中...', 'input': '请输入', 'required': '此字段不能为空', + 'checkboxOutOfRange': '请选择 %d 到 %d 个选项', }, } \ No newline at end of file