效果图:
校验部分的代码:
validateForm() {
let isFormValid = true;
let missingFields = [];
this.fieldList.forEach((field) => {
if (field.required && (!this.form[field.name] || this.form[field.name].trim().length === 0)) {
isFormValid = false;
missingFields.push(field.title);
}
});
if (!isFormValid) {
uni.showModal({
title: '错误提示',
content: `请确保以下必填项已填写:${missingFields.join('、')}`,
showCancel: false,
success: () => {},
});
return false;
}
return isFormValid;
},
点击提交按钮的会触发sub方法:
sub() {
if (!this.validateForm()) {
return;
}
// ... sub 方法原有的逻辑 ...
}