首頁 > 軟體

vue element el-form 多級巢狀驗證的實現範例

2022-08-12 14:00:10

最近在做專案時遇到這樣一個需求,一個form表單裡面有兩個欄位數量不固定,可以動態的增刪,在提交的時候不管數量有多少都需要驗證,頁面效果如下:

form表單對應的資料結構如下:

      voucherInfo: {
        cash: [
          {
            cashNum: '', // 押金流水號
            cashPayType: null, // 押金支付型別
          }
        ],
        cashPayTime: '', // 押金支付時間
        cashPayVoucher: [], // 押金支付憑證
        commissionNum: '', // 佣金流水號
        commissionPayType: null, // 佣金支付方式
        commissionPayTime: '', // 佣金支付時間
        commissionPayVoucher: [], // 佣金支付憑證
        remark: '' // 備註
      }

在這裡主要考慮的就是如何驗證voucherInfo的第一個欄位,它是一個陣列,陣列裡面又是一個物件,我們要驗證這個物件的每個屬性,簡而言之,就是驗證物件裡面的陣列裡面的物件屬性。

方法一:el-form裡面再巢狀一個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裡面的物件 -->
      	<!-- 又定義了一個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="'押金流水號' + (index + 1)"
              >
               <el-input
                v-model="item.cashNum"
                palceholder="請輸入"
               >
               </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="請選擇"
                >
                  <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="押金支付時間" prop="cashPayTime">
            <el-date-picker
              v-model="voucherInfo.cashPayTime"
              placeholder="請選擇"
            ></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="佣金流水號" prop="commissionNum">
            <el-input
              v-model="voucherInfo.commissionNum"
              placeholder="請輸入"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="佣金支付方式" prop="commissionPayType">
            <el-select
              v-model="voucherInfo.commissionPayType"
              placeholder="請選擇"
            >
              <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="佣金支付時間" prop="commissionPayTime">
            <el-date-picker
              v-model="voucherInfo.commissionPayTime"
              placeholder="請選擇"
            ></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="請輸入"
              v-model="voucherInfo.remark"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

驗證規則:

      voucherRule: {
        cashPayTime: [{ required: true, message: '請選擇押金支付時間', trigger: 'change'}],
        cashPayVoucher: [{ required: true, message: '請上傳押金支付憑證', trigger: 'change'}],
        commissionNum: [{ required: true, message: '請輸入佣金流水號', trigger: 'blur'}],
        commissionPayType: [{ required: true, message: '請選擇佣金支付方式', trigger: 'change'}],
        commissionPayTime: [{ required: true, message: '請選擇佣金支付時間', trigger: 'change'}],
        commissionPayVoucher: [{ required: true, message: '請上傳佣金支付憑證', trigger: 'change'}],
      },
      subVoucherRule: {
        cashNum: [{ required: true, message: '請輸入押金流水號', trigger: 'blur'}],
        cashPayType: [{ required: true, message: '請選擇押金支付方式', trigger: 'change'}],
      }

提交時驗證程式碼:因為有兩個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">
            	<!--注意有改動的是這裡   prop動態繫結cashNum   rules寫在了這裡 -->
              <el-form-item
                :prop="'cash['+index+'].cashNum'"
                :label="'押金流水號' + (index + 1)"
                :rules="{
                  required: true, message: '請輸入押金流水號', trigger: 'blur'
                }"
              >
               <el-input
                v-model="item.cashNum"
                palceholder="請輸入"
               >
               </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
            	<!--注意有改動的是這裡   prop動態繫結cashPayType   rules寫在了這裡 -->
              <el-form-item
                :label="'押金支付方式' + (index + 1)"
                :prop="'cash['+ index +'].cashPayType'"
                :rules="{
                  required: true, message: '請選擇押金支付方式', trigger: 'change'
                }"
              >
                <el-select
                  v-model="item.cashPayType"
                  placeholder="請選擇"
                >
                  <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="押金支付時間" prop="cashPayTime">
            <el-date-picker
              v-model="voucherInfo.cashPayTime"
              placeholder="請選擇"
            ></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="佣金流水號" prop="commissionNum">
            <el-input
              v-model="voucherInfo.commissionNum"
              placeholder="請輸入"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="佣金支付方式" prop="commissionPayType">
            <el-select
              v-model="voucherInfo.commissionPayType"
              placeholder="請選擇"
            >
              <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="佣金支付時間" prop="commissionPayTime">
            <el-date-picker
              v-model="voucherInfo.commissionPayTime"
              placeholder="請選擇"
            ></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="請輸入"
              v-model="voucherInfo.remark"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

方法二:直接把驗證規則寫在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">
            	<!--注意有改動的是這裡   prop動態繫結cashNum   rules寫在了這裡 -->
              <el-form-item
                :prop="'cash['+index+'].cashNum'"
                :label="'押金流水號' + (index + 1)"
                :rules="{
                  required: true, message: '請輸入押金流水號', trigger: 'blur'
                }"
              >
               <el-input
                v-model="item.cashNum"
                palceholder="請輸入"
               >
               </el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
            	<!--注意有改動的是這裡   prop動態繫結cashPayType   rules寫在了這裡 -->
              <el-form-item
                :label="'押金支付方式' + (index + 1)"
                :prop="'cash['+ index +'].cashPayType'"
                :rules="{
                  required: true, message: '請選擇押金支付方式', trigger: 'change'
                }"
              >
                <el-select
                  v-model="item.cashPayType"
                  placeholder="請選擇"
                >
                  <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="押金支付時間" prop="cashPayTime">
            <el-date-picker
              v-model="voucherInfo.cashPayTime"
              placeholder="請選擇"
            ></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="佣金流水號" prop="commissionNum">
            <el-input
              v-model="voucherInfo.commissionNum"
              placeholder="請輸入"
            >
            </el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="佣金支付方式" prop="commissionPayType">
            <el-select
              v-model="voucherInfo.commissionPayType"
              placeholder="請選擇"
            >
              <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="佣金支付時間" prop="commissionPayTime">
            <el-date-picker
              v-model="voucherInfo.commissionPayTime"
              placeholder="請選擇"
            ></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="請輸入"
              v-model="voucherInfo.remark"
            >
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

這樣驗證的時候只需要驗證一個表單就行了。
最終的實現效果:

 到此這篇關於vue element el-form 多級巢狀驗證的實現範例的文章就介紹到這了,更多相關element el-form 多級巢狀驗證內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com