vue一个界面有两个form表单如何进行校验

时间:2025-01-18 17:33:53
<template> <div style="width: 500px; margin: 0 auto"> <el-form :model="ruleFormA" :rules="rules" ref="ruleFormA"> <el-form-item label="活动名称" prop="name"> <el-input v-model=""></el-input> </el-form-item> <el-form-item label="活动区域" prop="region"> <el-select v-model="" placeholder="请选择活动区域" > <el-option label="区域一" value="shanghai"></el-option> <el-option label="区域二" value="beijing"></el-option> </el-select> </el-form-item> </el-form> <el-form :model="ruleFormB" :rules="rules" ref="ruleFormB"> <el-form-item label="特殊资源" prop="resource"> <el-radio-group v-model=""> <el-radio label="线上品牌商赞助"></el-radio> <el-radio label="线下场地免费"></el-radio> </el-radio-group> </el-form-item> <el-form-item label="活动形式" prop="desc"> <el-input type="textarea" v-model=""></el-input> </el-form-item> </el-form> <el-button type="primary" @click="submitForm">立即创建</el-button> </div> </template> <script> export default { data() { return { ruleFormA: { name: "", region: "", }, ruleFormB: { resource: "", desc: "", }, rules: { name: [ { required: true, message: "请输入活动名称", trigger: "blur", }, { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur", }, ], region: [ { required: true, message: "请选择活动区域", trigger: "change", }, ], resource: [ { required: true, message: "请选择活动资源", trigger: "change", }, ], desc: [ { required: true, message: "请填写活动形式", trigger: "blur", }, ], }, }; }, methods: { submitForm() { const rule1 = new Promise((resolve, reject) => { this.$refs["ruleFormA"].validate((valid) => { if (valid) { resolve(); } else { // reject('error submit!!') console.log("error submit!!"); return false; } }); }); const rule2 = new Promise((resolve, reject) => { this.$refs["ruleFormB"].validate((valid) => { if (valid) { resolve(); } else { console.log("error submit!!"); return false; } }); }); Promise.all([rule1, rule2]).then(() => { alert("submit!"); }); }, }, }; </script>