el-form 自定义验证规则,手动触发某项验证
1. ui
<el-form ref="xXXForm" :rules="XXXFormRules" >
<el-form-item label="图片" prop="xxx">
</el-form-item>
</el-form>
2. 变量初始化
export default {
data() {
return {
......
XXXFormRules: {
......
xxx: [
{
required: false,
validator: isEmptyRule,
trigger: 'change'
}
],
......
},
......
}
}
验证方法:
const isEmptyRule = (rule, value, callback) => {
if (value == null || value == "") {
callback(new Error('请上传图片')) // 一定要写 callBack()
} else {
callback()
}
};
3. 手动触发
this.$refs.xXXForm.validateField('xxx');