export const verifyFormData = (source, rules, verifyFn) => {
const keys = Object.keys(rules)
for (let i = 0; i < keys.length; i++) {
const key = keys[i]
let [result, message] = verifyItem(source[key], rules[key])
if (result && verifyFn && typeof verifyFn === 'function') {
const [fnResult, fnMsg] = verifyFn(source[key], rules[key], key)
result = result && fnResult
message = fnMsg || message
}
if (!result) {
uni.showToast({
title: message,
icon: "error"
})
return false
}
}
return true
}
用法:
data里
formData: {
projectName: "",
projectIntroduction: ""
}
rules: {
projectName: {
required: true,
message: '请输入项目名称'
},
projectIntroduction: {
required: true,
message: '请输入项目介绍'
},
}
methods里
let result = verifyFormData(this.formData(表单数据源), this.rules(规则), (source,rule,key,) => {
if(key == 'fileIdList') {
return [source.length > 0, '图片不能为空' ]
}
return [true]
});
if(!result) return;