vue 表单校验不通过时拦截提交表单
上代码:<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm"><el-form-item label="名称" prop="name"><el-input type="textarea" v-model="ruleForm.n
·
上代码:
<el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
<el-form-item label="名称" prop="name">
<el-input type="textarea" v-model="ruleForm.name"></el-input>
</el-form-item>
<el-form-item>
<el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
</el-form-item>
</el-form>
<script>
export default {
data() {
return {
ruleForm: {
name: ''
},
rules: {
name: [
{ required: true, message: '请输入活动名称', trigger: 'blur' },
{ min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
]
}
};
},
methods: {
submitForm(formName) {
this.$refs[formName].validate((valid) => { //开启校验
if (valid) { // 如果校验通过,请求接口,允许提交表单
alert('submit!');
} else { //校验不通过
return false;
}
});
}
}
}
</script>
更多推荐
已为社区贡献7条内容
所有评论(0)