這篇“vue element el-form多級(jí)嵌套驗(yàn)證如何實(shí)現(xiàn)”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“vue element el-form多級(jí)嵌套驗(yàn)證如何實(shí)現(xiàn)”文章吧。
專注于為中小企業(yè)提供成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站建設(shè)服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)永春免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動(dòng)了近千家企業(yè)的穩(wěn)健成長(zhǎng),幫助中小企業(yè)通過網(wǎng)站建設(shè)實(shí)現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。
最近在做項(xiàng)目時(shí)遇到這樣一個(gè)需求,一個(gè)form表單里面有兩個(gè)字段數(shù)量不固定,可以動(dòng)態(tài)的增刪,在提交的時(shí)候不管數(shù)量有多少都需要驗(yàn)證,頁面效果如下:
form表單對(duì)應(yīng)的數(shù)據(jù)結(jié)構(gòu)如下:
voucherInfo: { cash: [ { cashNum: '', // 押金流水號(hào) cashPayType: null, // 押金支付類型 } ], cashPayTime: '', // 押金支付時(shí)間 cashPayVoucher: [], // 押金支付憑證 commissionNum: '', // 傭金流水號(hào) commissionPayType: null, // 傭金支付方式 commissionPayTime: '', // 傭金支付時(shí)間 commissionPayVoucher: [], // 傭金支付憑證 remark: '' // 備注 }
在這里主要考慮的就是如何驗(yàn)證voucherInfo的第一個(gè)字段,它是一個(gè)數(shù)組,數(shù)組里面又是一個(gè)對(duì)象,我們要驗(yàn)證這個(gè)對(duì)象的每個(gè)屬性,簡(jiǎn)而言之,就是驗(yàn)證對(duì)象里面的數(shù)組里面的對(duì)象屬性。
<el-form ref="voucherForm" :rules="voucherRule" :model="voucherInfo" label-width="140px" > <div v-for="(item, index) in voucherInfo.cash" :key="index" > <!-- 嵌套的el-form model綁定的是voucherInfo.cash里面的對(duì)象 --> <!-- 又定義了一個(gè)rules :rules="subVoucherRule"--> <el-form ref="subVoucherForm" :model="item" :rules="subVoucherRule" label-width="140px" > <el-row> <el-col :span="6"> <el-form-item prop="cashNum" :label="'押金流水號(hào)' + (index + 1)" > <el-input v-model="item.cashNum" palceholder="請(qǐng)輸入" > </el-input> </el-form-item> </el-col> <el-col :span="12"> <el-form-item :label="'押金支付方式' + (index + 1)" prop="cashPayType" > <el-select v-model="item.cashPayType" placeholder="請(qǐng)選擇" > <el-option v-for="i in cashPayTypeOptions" :label="i.label" :value="i.value" :key="i.value" > </el-option> </el-select> </el-form-item> </el-col> <el-col :span="4"> <el-button type="primary" icon="el-icon-minus" circle @click="handleMinusClick(index)" > </el-button> <el-button type="primary" icon="el-icon-plus" circle @click="handleAddClick()" > </el-button> </el-col> </el-row> </el-form> </div> <el-row> <el-col :span="6"> <el-form-item label="押金支付時(shí)間" prop="cashPayTime"> <el-date-picker v-model="voucherInfo.cashPayTime" placeholder="請(qǐng)選擇" ></el-date-picker> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="上傳支付憑證" prop="cashPayVoucher"> <el-upload class="avatar-upload" action="" > <img v-if="voucherInfo.cashPayVoucher.length" src="" alt="" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="傭金流水號(hào)" prop="commissionNum"> <el-input v-model="voucherInfo.commissionNum" placeholder="請(qǐng)輸入" > </el-input> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="傭金支付方式" prop="commissionPayType"> <el-select v-model="voucherInfo.commissionPayType" placeholder="請(qǐng)選擇" > <el-option v-for="item in commissionPayTypeOptions" :label="item.label" :value="item.value" :key="item.value" ></el-option> </el-select> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="傭金支付時(shí)間" prop="commissionPayTime"> <el-date-picker v-model="voucherInfo.commissionPayTime" placeholder="請(qǐng)選擇" ></el-date-picker> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="傭金支付憑證" prop="commissionPayVoucher"> <el-upload class="avatar-upload" action="" > <img v-if="voucherInfo.commissionPayVoucher.length" src="" alt="" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <el-form-item label="備注"> <el-input type="textarea" placeholder="請(qǐng)輸入" v-model="voucherInfo.remark" > </el-input> </el-form-item> </el-col> </el-row> </el-form>
驗(yàn)證規(guī)則:
voucherRule: { cashPayTime: [{ required: true, message: '請(qǐng)選擇押金支付時(shí)間', trigger: 'change'}], cashPayVoucher: [{ required: true, message: '請(qǐng)上傳押金支付憑證', trigger: 'change'}], commissionNum: [{ required: true, message: '請(qǐng)輸入傭金流水號(hào)', trigger: 'blur'}], commissionPayType: [{ required: true, message: '請(qǐng)選擇傭金支付方式', trigger: 'change'}], commissionPayTime: [{ required: true, message: '請(qǐng)選擇傭金支付時(shí)間', trigger: 'change'}], commissionPayVoucher: [{ required: true, message: '請(qǐng)上傳傭金支付憑證', trigger: 'change'}], }, subVoucherRule: { cashNum: [{ required: true, message: '請(qǐng)輸入押金流水號(hào)', trigger: 'blur'}], cashPayType: [{ required: true, message: '請(qǐng)選擇押金支付方式', trigger: 'change'}], }
提交時(shí)驗(yàn)證代碼:因?yàn)橛袃蓚€(gè)form,所以兩個(gè)都需要驗(yàn)證
<el-form ref="voucherForm" :rules="voucherRule" :model="voucherInfo" label-width="140px" > <el-row v-for="(item, index) in voucherInfo.cash" :key="index" > <el-col :span="6"> <!--注意有改動(dòng)的是這里 prop動(dòng)態(tài)綁定cashNum rules寫在了這里 --> <el-form-item :prop="'cash['+index+'].cashNum'" :label="'押金流水號(hào)' + (index + 1)" :rules="{ required: true, message: '請(qǐng)輸入押金流水號(hào)', trigger: 'blur' }" > <el-input v-model="item.cashNum" palceholder="請(qǐng)輸入" > </el-input> </el-form-item> </el-col> <el-col :span="12"> <!--注意有改動(dòng)的是這里 prop動(dòng)態(tài)綁定cashPayType rules寫在了這里 --> <el-form-item :label="'押金支付方式' + (index + 1)" :prop="'cash['+ index +'].cashPayType'" :rules="{ required: true, message: '請(qǐng)選擇押金支付方式', trigger: 'change' }" > <el-select v-model="item.cashPayType" placeholder="請(qǐng)選擇" > <el-option v-for="i in cashPayTypeOptions" :label="i.label" :value="i.value" :key="i.value" > </el-option> </el-select> </el-form-item> </el-col> <el-col :span="4"> <el-button type="primary" icon="el-icon-minus" circle @click="handleMinusClick(index)" > </el-button> <el-button type="primary" icon="el-icon-plus" circle @click="handleAddClick()" > </el-button> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="押金支付時(shí)間" prop="cashPayTime"> <el-date-picker v-model="voucherInfo.cashPayTime" placeholder="請(qǐng)選擇" ></el-date-picker> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="上傳支付憑證" prop="cashPayVoucher"> <el-upload class="avatar-upload" action="" > <img v-if="voucherInfo.cashPayVoucher.length" src="" alt="" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="傭金流水號(hào)" prop="commissionNum"> <el-input v-model="voucherInfo.commissionNum" placeholder="請(qǐng)輸入" > </el-input> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="傭金支付方式" prop="commissionPayType"> <el-select v-model="voucherInfo.commissionPayType" placeholder="請(qǐng)選擇" > <el-option v-for="item in commissionPayTypeOptions" :label="item.label" :value="item.value" :key="item.value" ></el-option> </el-select> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="傭金支付時(shí)間" prop="commissionPayTime"> <el-date-picker v-model="voucherInfo.commissionPayTime" placeholder="請(qǐng)選擇" ></el-date-picker> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="傭金支付憑證" prop="commissionPayVoucher"> <el-upload class="avatar-upload" action="" > <img v-if="voucherInfo.commissionPayVoucher.length" src="" alt="" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <el-form-item label="備注"> <el-input type="textarea" placeholder="請(qǐng)輸入" v-model="voucherInfo.remark" > </el-input> </el-form-item> </el-col> </el-row> </el-form>
<el-form ref="voucherForm" :rules="voucherRule" :model="voucherInfo" label-width="140px" > <el-row v-for="(item, index) in voucherInfo.cash" :key="index" > <el-col :span="6"> <!--注意有改動(dòng)的是這里 prop動(dòng)態(tài)綁定cashNum rules寫在了這里 --> <el-form-item :prop="'cash['+index+'].cashNum'" :label="'押金流水號(hào)' + (index + 1)" :rules="{ required: true, message: '請(qǐng)輸入押金流水號(hào)', trigger: 'blur' }" > <el-input v-model="item.cashNum" palceholder="請(qǐng)輸入" > </el-input> </el-form-item> </el-col> <el-col :span="12"> <!--注意有改動(dòng)的是這里 prop動(dòng)態(tài)綁定cashPayType rules寫在了這里 --> <el-form-item :label="'押金支付方式' + (index + 1)" :prop="'cash['+ index +'].cashPayType'" :rules="{ required: true, message: '請(qǐng)選擇押金支付方式', trigger: 'change' }" > <el-select v-model="item.cashPayType" placeholder="請(qǐng)選擇" > <el-option v-for="i in cashPayTypeOptions" :label="i.label" :value="i.value" :key="i.value" > </el-option> </el-select> </el-form-item> </el-col> <el-col :span="4"> <el-button type="primary" icon="el-icon-minus" circle @click="handleMinusClick(index)" > </el-button> <el-button type="primary" icon="el-icon-plus" circle @click="handleAddClick()" > </el-button> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="押金支付時(shí)間" prop="cashPayTime"> <el-date-picker v-model="voucherInfo.cashPayTime" placeholder="請(qǐng)選擇" ></el-date-picker> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="上傳支付憑證" prop="cashPayVoucher"> <el-upload class="avatar-upload" action="" > <img v-if="voucherInfo.cashPayVoucher.length" src="" alt="" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="傭金流水號(hào)" prop="commissionNum"> <el-input v-model="voucherInfo.commissionNum" placeholder="請(qǐng)輸入" > </el-input> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="傭金支付方式" prop="commissionPayType"> <el-select v-model="voucherInfo.commissionPayType" placeholder="請(qǐng)選擇" > <el-option v-for="item in commissionPayTypeOptions" :label="item.label" :value="item.value" :key="item.value" ></el-option> </el-select> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="6"> <el-form-item label="傭金支付時(shí)間" prop="commissionPayTime"> <el-date-picker v-model="voucherInfo.commissionPayTime" placeholder="請(qǐng)選擇" ></el-date-picker> </el-form-item> </el-col> <el-col :span="12"> <el-form-item label="傭金支付憑證" prop="commissionPayVoucher"> <el-upload class="avatar-upload" action="" > <img v-if="voucherInfo.commissionPayVoucher.length" src="" alt="" class="avatar"> <i v-else class="el-icon-plus avatar-uploader-icon"></i> </el-upload> </el-form-item> </el-col> </el-row> <el-row> <el-col :span="12"> <el-form-item label="備注"> <el-input type="textarea" placeholder="請(qǐng)輸入" v-model="voucherInfo.remark" > </el-input> </el-form-item> </el-col> </el-row> </el-form>
這樣驗(yàn)證的時(shí)候只需要驗(yàn)證一個(gè)表單就行了。
最終的實(shí)現(xiàn)效果:
以上就是關(guān)于“vue element el-form多級(jí)嵌套驗(yàn)證如何實(shí)現(xiàn)”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
分享名稱:vue?element?el-form多級(jí)嵌套驗(yàn)證如何實(shí)現(xiàn)
鏈接URL:http://jinyejixie.com/article44/ijjsee.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站導(dǎo)航、關(guān)鍵詞優(yōu)化、服務(wù)器托管、微信公眾號(hào)、網(wǎng)站排名、面包屑導(dǎo)航
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)