91超碰碰碰碰久久久久久综合_超碰av人澡人澡人澡人澡人掠_国产黄大片在线观看画质优化_txt小说免费全本

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

vue?element?el-form多級嵌套驗證如何實現

發布時間:2022-08-12 14:27:19 來源:億速云 閱讀:185 作者:iii 欄目:服務器

這篇“vue element el-form多級嵌套驗證如何實現”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“vue element el-form多級嵌套驗證如何實現”文章吧。

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

vue?element?el-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多級嵌套驗證如何實現

以上就是關于“vue element el-form多級嵌套驗證如何實現”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

平凉市| 兴业县| 甘谷县| 原阳县| 广南县| 榆中县| 司法| 沾益县| 开远市| 高密市| 涟源市| 宜章县| 准格尔旗| 滦南县| 监利县| 漠河县| 新和县| 汕尾市| 通渭县| 望奎县| 神木县| 安国市| 庆云县| 黄梅县| 阆中市| 比如县| 民乐县| 张家川| 当涂县| 宜丰县| 潮州市| 阳山县| 莒南县| 新密市| 卢氏县| 江西省| 万荣县| 平陆县| 元谋县| 开封县| 柞水县|