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

粘貼可用的element-ui validateField局部校驗(yàn)

 更新時間:2023年01月12日 09:22:26   作者:qb  
這篇文章主要為大家介紹了粘貼可用element-ui中validateField局部校驗(yàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jì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)文章

  • axios請求中斷的幾種方法

    axios請求中斷的幾種方法

    在實(shí)際應(yīng)用場景中,假如有一個下載或者導(dǎo)出請求,數(shù)據(jù)量非常大的情況下,接口響應(yīng)的會很慢,這時候我我們想中斷請求,該怎么做呢?本文給大家介紹了axios請求中斷的幾種方法,需要的朋友可以參考下
    2024-10-10
  • mpvue跳轉(zhuǎn)頁面及注意事項(xiàng)

    mpvue跳轉(zhuǎn)頁面及注意事項(xiàng)

    這篇文章主要介紹了mpvue跳轉(zhuǎn)頁面及注意事項(xiàng)的相關(guān)資料,需要的朋友可以參考下
    2018-08-08
  • 在vue項(xiàng)目中引用Iview的方法

    在vue項(xiàng)目中引用Iview的方法

    iView 是一套基于 Vue.js 的開源 UI 組件庫,主要服務(wù)于 PC 界面的中后臺產(chǎn)品。這篇文章主要介紹了在vue項(xiàng)目中引用Iview的方法,需要的朋友可以參考下
    2018-09-09
  • vite基本常見的配置講解

    vite基本常見的配置講解

    這篇文章主要給大家介紹了關(guān)于vite基本常見配置講解的相關(guān)資料,最近做項(xiàng)目要求將webpack打包方式換成vite,下面將詳細(xì)講解一下配置vite需要改動的東西,需要的朋友可以參考下
    2023-11-11
  • Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能

    Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能

    這篇文章主要介紹了Vue框架+Element-ui(el-upload組件)使用http-request方法上傳文件并顯示文件上傳進(jìn)度功能,本通過實(shí)例代碼給大家介紹的非常詳細(xì),感興趣的朋友跟隨小編一起看看吧
    2024-08-08
  • vue結(jié)合axios實(shí)現(xiàn)restful風(fēng)格的四種請求方式

    vue結(jié)合axios實(shí)現(xiàn)restful風(fēng)格的四種請求方式

    這篇文章主要介紹了vue結(jié)合axios實(shí)現(xiàn)restful風(fēng)格的四種請求方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • 詳解vue跨組件通信的幾種方法

    詳解vue跨組件通信的幾種方法

    本篇文章主要介紹了詳解vue跨組件通信的幾種方法 ,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • Vue 頁面切換效果之 BubbleTransition(推薦)

    Vue 頁面切換效果之 BubbleTransition(推薦)

    使用 vue,vue-router,animejs 來講解如何實(shí)現(xiàn)vue頁面切換效果之 BubbleTransition,需要的朋友參考下吧
    2018-04-04
  • VUE配置proxy代理的開發(fā)測試及生產(chǎn)環(huán)境

    VUE配置proxy代理的開發(fā)測試及生產(chǎn)環(huán)境

    這篇文章主要為大家介紹了VUE配置proxy代理的開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-08-08
  • 一文詳解Vue中的虛擬DOM與Diff算法

    一文詳解Vue中的虛擬DOM與Diff算法

    vue中的diff算法時常是面試過程中的考點(diǎn),本文將為大家講解何為diff以及diff算法的實(shí)現(xiàn)過程,那么在了解diff之前,我們需要先了解虛擬DOM是什么,需要的朋友可以參考下
    2024-02-02

最新評論