欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue element el-form 多級(jí)嵌套驗(yàn)證的實(shí)現(xiàn)示例

 更新時(shí)間:2022年08月12日 11:52:54   作者:zhang070514  
本文主要介紹了vue element el-form 多級(jí)嵌套驗(yàn)證的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

最近在做項(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里面再嵌套一個(gè)el-form

  <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>

方法二:直接把驗(yàn)證規(guī)則寫在html中

 <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)示例的文章就介紹到這了,更多相關(guān)element el-form 多級(jí)嵌套驗(yàn)證內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論