粘貼可用的element-ui validateField局部校驗(yàn)
正文
element-ui
的表單提交基本每個項(xiàng)目都會使用到,其中針對特定區(qū)域的值進(jìn)行校驗(yàn)的場景也不少,其文檔相對簡單,下面就貼上代碼,復(fù)制粘貼到一個.vue
文件即可運(yùn)行。
場景:人員信息錄入時,表單可以就其中某一行進(jìn)行保存,也可創(chuàng)建多個后批量保存,保存時如果信息不全會只針對當(dāng)前行進(jìn)行校驗(yàn)。
1、template
<template> <el-form :model="dynamicValidateForm" ref="dynamicValidateForm" label-width="80px"> <div v-for="(domain, index) in dynamicValidateForm.domains" :key="index" class="form-item" > <el-form-item> {{ index + 1 }} </el-form-item> <el-form-item label="姓名" size="mini" :prop="'domains.' + index + '.name'" :rules="{ required: true, message: '姓名不能為空', trigger: 'blur', }" > <el-input v-model="domain.name"></el-input> </el-form-item> <el-form-item label="年齡" size="mini" :prop="'domains.' + index + '.age'" :rules="{ required: true, message: '年齡不能為空', trigger: 'blur', }" > <el-input v-model="domain.age"></el-input> </el-form-item> <el-form-item label="地址" size="mini" :prop="'domains.' + index + '.address'" :rules="{ required: true, message: '地址不能為空', trigger: 'blur', }" > <el-input v-model="domain.address"></el-input> </el-form-item> <el-form-item label="手機(jī)" size="mini" :prop="'domains.' + index + '.phone'" :rules="{ required: true, message: '手機(jī)不能為空', trigger: 'blur', }" > <el-input v-model="domain.phone"></el-input> </el-form-item> <el-form-item> <el-button size="mini" type="primary" @click="saveCurrentData(domain, index)" >保存</el-button > </el-form-item> </div> <el-form-item class="form-footer"> <el-button type="primary" size="mini" @click="submitForm('dynamicValidateForm')" >全部保存</el-button > <el-button size="mini" @click="resetForm('dynamicValidateForm')">重置</el-button> </el-form-item> </el-form> </template>
2、script
<script> export default { data() { return { dynamicValidateForm: { domains: [], }, }; }, created() { const item = { name: "", age: "", address: "", phone: "", }; for (let i = 0; i < 5; i++) { this.dynamicValidateForm.domains.push(Object.assign({}, item)); } }, methods: { // 定義局部校驗(yàn)函數(shù) validateFn(domain, index) { let validateFieldArr = ["name", "age", "address", "phone"]; return new Promise((resolve, reject) => { for (let item of validateFieldArr) { this.$refs.dynamicValidateForm.validateField( `domains.${index}.${item}`, (err) => { if (err) { return reject(err); // 失敗返回err } resolve(true); // 成功返回true } ); } }); }, async saveCurrentData(domain, index) { const validateResult = await this.validateFn(domain, index); if (validateResult) { console.log("-------校驗(yàn)通過啦------"); } }, submitForm(formName) { this.$refs[formName].validate((valid) => { if (valid) { alert("submit!"); } else { return false; } }); }, resetForm(formName) { this.$refs[formName].resetFields(); }, }, }; </script>
3、style
<style> .form-item { display: flex; } .form-footer { display: flex; justify-content: center; } </style>
以上就是粘貼可用element-ui中validateField局部校驗(yàn)的詳細(xì)內(nèi)容,更多關(guān)于element-ui validateField局部校驗(yàn)的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能
這篇文章主要介紹了Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能,本通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧2024-08-08vue結(jié)合axios實(shí)現(xiàn)restful風(fēng)格的四種請求方式
這篇文章主要介紹了vue結(jié)合axios實(shí)現(xiàn)restful風(fēng)格的四種請求方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06Vue 頁面切換效果之 BubbleTransition(推薦)
使用 vue,vue-router,animejs 來講解如何實(shí)現(xiàn)vue頁面切換效果之 BubbleTransition,需要的朋友參考下吧2018-04-04VUE配置proxy代理的開發(fā)測試及生產(chǎn)環(huán)境
這篇文章主要為大家介紹了VUE配置proxy代理的開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08