表单验证以及踩坑
this.$refs["ruleForm"].validate((valid) => {
if(valid){
}else{
console.log("error submit!!");
return false;
}
}
- 单个验证
this.$refs["ruleForm"].validateField("phone", (valid) => {
if(valid){
}else{
console.log("error submit!!");
return false;
}
}
- 多个验证
this.$refs["ruleForm"].validateField(['username','password'], (valid) => {
if(valid){
}else{
console.log("error submit!!");
return false;
}
}
上述验证方法你验证几个就会发起几次请求,暂时还没有想到解决方案
handleClick(){
let that = this;
let validData = {
username:that.ruleForm.username,
loginpassword:that.ruleForm.loginpassword
};
if (validData .username == '' || validData .loginpassword == '') {
this.$refs["ruleForm"].validateField("phone");
this.$refs["ruleForm"].validateField("loginpassword");
} else {
this.$api.login.post().then((res)=>{}).catch(()={})
})
}
上述验证方法可以暂时解决,还没有其他更好的思路,等后面有更好方案继续更新