element 自定义form表单校验规则导致validate不执行
1.错误代码
下面的代码看起来似乎是没什么问题,控制台与编译器都没有报错,可是validate就是不执行。
data() {
var checkSignatoryLength = (rule, value, callback) => {
if (this.form.signatory !== undefined && this.form.signatory !== null) {
if (this.form.signatory.length < 2) {
return callback(new Error('需至少选择两个签字人'));
}
}
};
return {
rules{
signatory: [
{ required: true, message: '请选择签字人', trigger: 'change' },
{ required: true, validator: checkSignatoryLength, trigger: 'change' }
]
}
}
}
2.解决办法
校验代码添加else返回callback();
data() {
var checkSignatoryLength = (rule, value, callback) => {
if (this.form.signatory !== undefined && this.form.signatory !== null) {
if (this.form.signatory.length < 2) {
return callback(new Error('需至少选择两个签字人'));
} else {
callback();
}
}
};
return {
rules{
signatory: [
{ required: true, message: '请选择签字人', trigger: 'change' },
{ required: true, validator: checkSignatoryLength, trigger: 'change' }
]
}
}
}
3.原因
自定义校验规则无论校验是否通过都一定要返回callback()