最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue動(dòng)態(tài)添加表單validateField驗(yàn)證功能實(shí)現(xiàn)

 更新時(shí)間:2023年04月07日 10:06:53   作者:totau  
這篇文章主要介紹了vue動(dòng)態(tài)添加表單validateField驗(yàn)證功能實(shí)現(xiàn),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

vue動(dòng)態(tài)添加表單validateField驗(yàn)證,代碼如下所示:

<template>
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <div v-for="(input, index) in inputs" :key="index">
        <el-form-item :label="'Name ' + (index + 1)" :prop="'name' + index">
          <el-input v-model="input.name" @blur="validateName(index)"></el-input>
        </el-form-item>
        <el-form-item :label="'Age ' + (index + 1)" :prop="'age' + index">
          <el-input v-model.number="input.age" @blur="validateAge(index)"></el-input>
        </el-form-item>
      </div>
      <el-button type="primary" @click="addInput">Add input</el-button>
      <el-button type="primary" @click="submitForm">Submit</el-button>
    </el-form>
  </template>

  <script>
  export default {
    data() {
      return {
        form: {},
        inputs: [{name: '',age: ''}],
        rules: {
            name0: { required: true, pattern: /^[A-Za-z]+$/, message: 'Name can only contain letters', trigger: 'blur' },
            age0: { required: true, pattern: /^\d+$/, message: 'Age can only contain numbers', trigger: 'blur' },
        }
      }
    },
    methods: {
      addInput() {
        const index = this.inputs.length
        this.inputs.push({ name: '', age: '' })
        this.$set(this.form, `name${index}`, '')
        this.$set(this.form, `age${index}`, '')
        this.$set(this.rules, `name${index}`, { required: true, pattern: /^[A-Za-z]+$/, message: 'Name can only contain letters', trigger: 'blur' })
        this.$set(this.rules, `age${index}`, { required: true, pattern: /^\d+$/, message: 'Age can only contain numbers', trigger: 'blur' })
      },
      validateName(index) {
        this.$refs.form.validateField(`name${index}`)
      },
      validateAge(index) {
        this.$refs.form.validateField(`age${index}`)
      },
      submitForm() {
        this.$refs.form.validate(valid => {
          if (valid) {
            // submit form
          } else {
            console.log('validation failed')
          }
        })
      }
    }
  }
  </script>

可以使用 Element UI 的表單組件結(jié)合 Vue 的動(dòng)態(tài)組件來實(shí)現(xiàn)動(dòng)態(tài)添加多組輸入框,并對每個(gè)輸入框進(jìn)行校驗(yàn)。Element UI 提供了很多內(nèi)置的校驗(yàn)規(guī)則和提示信息,可以方便地應(yīng)用到表單中。

首先,我們需要在 Vue 實(shí)例中聲明一個(gè) inputs 數(shù)組來存儲(chǔ)每個(gè)輸入組的數(shù)據(jù)。在添加輸入組時(shí),我們只需要向 inputs 數(shù)組中添加一個(gè)新的對象即可。

在模板中,我們使用 Element UI 的表單組件來渲染輸入框,并使用 v-for 指令循環(huán)渲染多組輸入框。在每個(gè)輸入框中,我們使用 v-model 指令將輸入值綁定到 inputs 數(shù)組中的數(shù)據(jù)屬性上。對于 name 和 age 輸入框,我們使用 pattern 規(guī)則來進(jìn)行校驗(yàn),并在 rules 對象中提供了相應(yīng)的錯(cuò)誤提示信息。在 checkNameInput 和 checkAgeInput 方法中,我們調(diào)用 $refs.form.validateField 方法來手動(dòng)觸發(fā)校驗(yàn),并將當(dāng)前輸入對象作為參數(shù)傳遞進(jìn)去。

最后,我們需要在 Vue 實(shí)例中聲明一個(gè) form 對象來維護(hù)表單數(shù)據(jù),并將 rules 對象傳遞給 el-form 組件的 rules 屬性。這樣,每次輸入框的值發(fā)生變化時(shí),就會(huì)自動(dòng)觸發(fā) Element UI 的校驗(yàn)機(jī)制,并顯示相應(yīng)的錯(cuò)誤提示信息。

補(bǔ)充:vue動(dòng)態(tài)表單添加表單驗(yàn)證

            <el-form ref="conditionListForm" :rules="ConditionListRules" :model="scope.row.conditionListForm">
              <el-table
                ref="conditionListDia"
                :data="scope.row.conditionListForm.conditionListDia"
                border
                :header-cell-style="background"
                style="width: 100%"
                size="mini"
              >
                <el-table-column label="限制條件名稱" align="center">
                  <el-table-column
                    label="條件組合"
                    align="center"
                    size="mini"
                    min-width="40"
                  >
                    <template slot-scope="scopeChild">
                      <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionRelation' " :rules="ConditionListRules.conditionRelation">
                        <el-select
                          v-model="scopeChild.row.conditionRelation"
                          :disabled="scope.row.conditionListForm.conditionListDia.length > 1 || scope.row.id > 0"
                          size="mini"
                          placeholder="請選擇"
                          clearable
                          style="float:left;width:95%"
                          @change="showconditionRelation(scopeChild.row.conditionRelation)"
                        >
                          <el-option
                            v-for="item in compositionConditionList"
                            :key="item.key"
                            :disabled="item.baseDataStatus==='0'"
                            :label="item.label"
                            :value="item.key"
                          />
                        </el-select>
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="條件分組"
                    size="mini"
                    align="center"
                    min-width="30"
                  >
                    <template slot-scope="scopeChild">
                      <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].medalConditionClassname' " :rules="ConditionListRules.medalConditionClassname">
                        <el-select
                          v-model="scopeChild.row.medalConditionClassname"
                          :disabled="scope.row.id > 0"
                          size="mini"
                          placeholder="請選擇"
                          clearable
                          style="float:left;width:95%"
                          @change="defDataClick(scopeChild)"
                        >
                          <el-option
                            v-for="item in fatherNameList"
                            :key="item.id"
                            :label="item.medalConditionClassname"
                            :value="item.id"
                          />
                        </el-select>
                      </el-form-item>
                    </template>
                  </el-table-column>
                  <el-table-column
                    label="條件"
                    size="mini"
                    align="center"
                    min-width="40"
                  >
                    <template slot-scope="scopeChild">
                      <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionId' " :rules="ConditionListRules.conditionId">
                        <el-select
                          v-model="scopeChild.row.conditionId"
                          size="mini"
                          :disabled="scope.row.id > 0"
                          placeholder="請選擇"
                          clearable
                          style="float:left;width:95%"
                          @change="getRelationship(scopeChild.row.conditionId,scopeChild.row.medalConditionClassnameList,scopeChild.row)"
                        >
                          <el-option
                            v-for="item in scopeChild.row.medalConditionClassnameList"
                            :key="item.defId"
                            :label="item.medalConditionName"
                            :value="item.defId"
                          />
                        </el-select>
                      </el-form-item>
                    </template>
                  </el-table-column>
                </el-table-column>
                <el-table-column
                  label="運(yùn)算關(guān)系"
                  align="center"
                  size="mini"
                  min-width="50"
                >
                  <template slot-scope="scopeChild">
                    <el-form-item :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionOperation' " :rules="ConditionListRules.conditionOperation">
                      <el-select
                        v-model="scopeChild.row.conditionOperation"
                        size="mini"
                        placeholder="請選擇"
                        :disabled="scope.row.id > 0"
                        clearable
                        style="float:left;width:95%"
                        @change="conditionOperationEmpty(scopeChild.row)"
                      >
                        <el-option
                          v-for="item in scopeChild.row.conditionOperationValueList"
                          :key="item.key"
                          :disabled="item.baseDataStatus==='0'"
                          :label="item.value"
                          :value="item.key"
                        />
                      </el-select>
                    </el-form-item>
                  </template>
                </el-table-column>
                <el-table-column
                  label="值"
                  align="center"
                  size="mini"
                  min-width="120"
                >
                  <template slot-scope="scopeChild">
                    <el-form-item v-if="scopeChild.row.medalConditionControlType === '0'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionTrueValueList' " :rules="ConditionListRules.conditionTrueValueList">
                      <el-select
                        v-model="scopeChild.row.conditionTrueValueList"
                        multiple
                        style="float:left;width:95%"
                        size="mini"
                        clearable
                        placeholder="請選擇"
                        @change="conditionValueEmpty(scopeChild)"
                      >
                        <el-option
                          v-for="item in scopeChild.row.conditionValueList"
                          :key="item.value"
                          :label="item.key"
                          :value="item.value"
                        />
                      </el-select>
                    </el-form-item>
                    <el-form-item v-if="scopeChild.row.medalConditionControlType == '1'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionValue' " :rules="ConditionListRules.conditionValue">
                      <el-input
                        v-model="scopeChild.row.conditionValue"
                        clearable
                        placeholder="請輸入"
                        align="center"
                        style="float:left;"
                        size="mini"
                        maxlength="60"
                        show-word-limit
                        @blur="conditionValueEmpty(scopeChild)"
                      />
                    </el-form-item>
                    <el-form-item v-if="scopeChild.row.medalConditionControlType == '2'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionTrueValueList' " :rules="ConditionListRules.conditionTrueValueList">
                      <el-date-picker
                        v-model="scopeChild.row.conditionTrueValueList"
                        type="daterange"
                        range-separator="至"
                        start-placeholder="開始日期"
                        size="mini"
                        end-placeholder="結(jié)束日期"
                        style="float:left;width:95%"
                      />
                    </el-form-item>
                    <el-form-item v-if="scopeChild.row.medalConditionControlType === '3'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionValue' " :rules="ConditionListRules.conditionValue">
                      <el-select
                        v-model="scopeChild.row.conditionValue"
                        style="float:left;width:95%"
                        size="mini"
                        clearable
                        placeholder="請選擇"
                        @change="conditionValueEmpty(scopeChild)"
                      >
                        <el-option
                          v-for="item in scopeChild.row.conditionValueList"
                          :key="item.value"
                          :label="item.key"
                          :value="item.value"
                        />
                      </el-select>
                    </el-form-item>
                    <el-form-item v-if="scopeChild.row.medalConditionControlType === '4'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionTrueValueList' " :rules="ConditionListRules.conditionTrueValueList">
                      <el-time-picker
                        v-model="scopeChild.row.conditionTrueValueList"
                        style="float:left;width:95%"
                        size="mini"
                        is-range
                        clearable
                        range-separator="至"
                        start-placeholder="開始時(shí)間"
                        end-placeholder="結(jié)束時(shí)間"
                        placeholder="選擇時(shí)間范圍"
                        format="HH:mm"
                        @change="conditionValueEmpty(scopeChild)"
                      />
                    </el-form-item>
                    <el-form-item v-if="scopeChild.row.medalConditionControlType == '5'" :prop=" 'conditionListDia[' + scopeChild.$index + '].conditionValue' " :rules="ConditionListRules.conditionValue">
                      <el-input-number v-model="scopeChild.row.conditionValue" size="mini" :min="0" label="" @change="conditionValueEmpty(scope)" />
                    </el-form-item>

                  </template>
                </el-table-column>
                <el-table-column
                  label="操作"
                  align="center"
                  min-width="50"
                >
                  <template slot-scope="scopeChild">
                    <el-button type="text" :disabled="scope.row.id > 0" @click="delCondition(scope.$index,scope.row.conditionListForm.conditionListDia,scopeChild.row,scopeChild.$index)">刪除</el-button>
                  </template>
                </el-table-column>
              </el-table>
            </el-form>
 ConditionListRules: {
    conditionRelation: [
      { required: true, message: '條件組合不能為空', trigger: 'change' }
    ],
    medalConditionClassname: [
      { required: true, message: '條件分組不能為空', trigger: 'change' }
    ],
    conditionId: [
      { required: true, message: '條件不能為空', trigger: 'change' }
    ],
    conditionOperation: [
      { required: true, message: '運(yùn)算關(guān)系不能為空', trigger: 'change' }
    ],
    conditionValue: [
      { required: true, message: '值不能為空', trigger: 'change' }
    ],
    conditionTrueValueList: [
      { required: true, message: '值不能為空', trigger: 'change' }
    ]
  },

主要就是prop的問題,prop要對應(yīng)到數(shù)據(jù)。

到此這篇關(guān)于vue動(dòng)態(tài)添加表單validateField驗(yàn)證的文章就介紹到這了,更多相關(guān)vue動(dòng)態(tài)添加表單內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue?事件獲取當(dāng)前組件的屬性方式

    vue?事件獲取當(dāng)前組件的屬性方式

    這篇文章主要介紹了vue?事件獲取當(dāng)前組件的屬性方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Vue實(shí)現(xiàn)進(jìn)度條變化效果

    Vue實(shí)現(xiàn)進(jìn)度條變化效果

    這篇文章主要為大家詳細(xì)介紹了Vue實(shí)現(xiàn)進(jìn)度條變化效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • vue頁面跳轉(zhuǎn)后返回原頁面初始位置方法

    vue頁面跳轉(zhuǎn)后返回原頁面初始位置方法

    下面小編就為大家分享一篇vue頁面跳轉(zhuǎn)后返回原頁面初始位置方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • vue動(dòng)態(tài)的 BreadCrumb 組件el-breadcrumb ElementUI詳解

    vue動(dòng)態(tài)的 BreadCrumb 組件el-breadcrumb ElementUI詳解

    這篇文章主要介紹了vue如何做一個(gè)動(dòng)態(tài)的 BreadCrumb 組件,el-breadcrumb ElementUI
    ,本文通過圖文示例代碼相結(jié)合給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • Vue如何設(shè)置button的disable屬性

    Vue如何設(shè)置button的disable屬性

    這篇文章主要介紹了Vue如何設(shè)置button的disable屬性,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-04-04
  • 淺談Vue3中key的作用和工作原理

    淺談Vue3中key的作用和工作原理

    本文主要介紹了Vue3中key的作用和工作原理,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • Element Alert警告的具體使用方法

    Element Alert警告的具體使用方法

    這篇文章主要介紹了Element Alert警告的具體使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • 詳解Vue2.x-directive的學(xué)習(xí)筆記

    詳解Vue2.x-directive的學(xué)習(xí)筆記

    這篇文章主要介紹了詳解Vue2.x-directive的學(xué)習(xí)筆記,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-07-07
  • 關(guān)于el-scrollbar滾動(dòng)條初始化不顯示的問題及解決

    關(guān)于el-scrollbar滾動(dòng)條初始化不顯示的問題及解決

    這篇文章主要介紹了關(guān)于el-scrollbar滾動(dòng)條初始化不顯示的問題及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue使用Vue.extend方法仿寫個(gè)loading加載中效果實(shí)例

    vue使用Vue.extend方法仿寫個(gè)loading加載中效果實(shí)例

    在vue中提供v-loading命令,用于div的loading加載,下面這篇文章主要給大家介紹了關(guān)于vue使用Vue.extend方法仿寫個(gè)loading加載中效果的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-06-06

最新評(píng)論

洪江市| 辽宁省| 乐昌市| 怀安县| 新郑市| 中宁县| 合作市| 临漳县| 永定县| 五常市| 伊春市| 忻城县| 清涧县| 灵川县| 金沙县| 阿克陶县| 靖边县| 白银市| 麻栗坡县| 油尖旺区| 普陀区| 神木县| 兰考县| 鄂伦春自治旗| 五莲县| 尚义县| 蕉岭县| 宜阳县| 会宁县| 梁山县| 花莲市| 菏泽市| 佛坪县| 高雄县| 临安市| 梁河县| 娱乐| 连云港市| 惠来县| 城口县| 原阳县|