默认校验规则
pid: { required: true, message: '请选择', trigger: 'change' },
需要实现的
场景: 下拉的时候根据接口判断用户的选择是否已存在
data(){
var validateSid = (rule, value, callback)=>{ if(value == ''){ console.log('0'); callback(new Error('请选择所属项目')) }else if(this.sidMess != ''){ console.log('0'); callback(new Error(this.sidMess)) }else{ console.log('0'); callback() } }
return{
sidMess: '',
rules:{
sid: { validator:validateSid, trigger: 'change'},
}
}
}
async plateChange(val) {this.sidMess = '' this.platformList.forEach(item => { if (item.sid == val) { this.addForm.projectName = item.name } }) let res = await get(`xxxxxx?sid=${this.addForm.sid}`) let {code,msg} = res if(code==0){ this.sidMess = '' } else{ this.sidMess = msg this.$refs.addForm.validateField('sid') } },