本文介绍: 同时提交两个表单时,需校验表单时,可通过Promise.all 控制两个表单校验成功时才提交

同时提交两个表单时,需校验表单时,可通过Promise.all 控制两个表单校验成功时才提交

注意:当两个form使用一个ref 或者同一个model每个formitem下的校验提示失效

<el-tabs>
	<el-tab-pane label="基本信息">
		<el-form :model="state.basicForm" ref="basicFormRef" size="default" label-width="100px" :disabled="state.formDisabled">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
					<el-form-item label="编号" prop="personalInfoNum" :rules="[{ required: true, message: '编号不能为空', trigger: 'blur' }]">
						<el-input v-model="state.basicForm.personalInfoNum" placeholder="编号" clearable />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</el-tab-pane>
	<el-tab-pane label="其他信息">
		<el-form :model="state.otherForm" ref="otherFormRef" size="default" label-width="100px" :disabled="state.formDisabled">
			<el-row :gutter="35">
				<el-col :xs="24" :sm="8" :md="8" :lg="8" :xl="8" class="mb20">
					<el-form-item label="奖惩情况" prop="reward" :rules="[{ required: true, message: '奖惩情况不能为空', trigger: 'blur' }]">
						<el-input v-model="state.otherForm.reward" clearable type="textarea" autosize />
					</el-form-item>
				</el-col>
			</el-row>
		</el-form>
	</el-tab-pane>
</el-tabs>
<el-row>
	<el-col :md="24" :sm="24" style="text-align: center; margin-top: 20px">
		<el-button type="primary" size="large" class="button" @click="submit">提交</el-button>
	</el-col>
</el-row>
<script lang="ts" setup name="person-add">
import { ref, reactive,} from 'vue';
const basicFormRef = ref();
const otherFormRef= ref();
const state=reactive({
	formDisabled: false, //表单整个禁用,新增/查看
	basicForm: {
		personalInfoNum:null
	},
	otherForm:{
		reward:''
	}
})
//检查每个表单校验规则
const checkForm = (formName: any) => {
	return new Promise((resolve, reject) => {
		formName.value.validate((valid: boolean) => {
			if (valid) {
				resolve(1);
			} else reject();
		});
	});
};
// 提交
const submit = () => {
	let list = [];
	list.push(checkForm(basicFormRef), checkForm(otherFormRef));

	Promise.all(list)
		.then(async () => {
			await addPersonalInfo(parmas);//调用接口
		})
}
</script>

原文地址:https://blog.csdn.net/MSHK111/article/details/131889259

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任

如若转载,请注明出处:http://www.7code.cn/show_18837.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱suwngjj01@126.com进行投诉反馈,一经查实,立即删除

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注