vue动态添加表单validateField验证功能实现

作者:totau 时间:2023-07-02 17:03:27 

vue动态添加表单validateField验证,代码如下所示:

<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 的表单组件结合 Vue 的动态组件来实现动态添加多组输入框,并对每个输入框进行校验。Element UI 提供了很多内置的校验规则和提示信息,可以方便地应用到表单中。

首先,我们需要在 Vue 实例中声明一个 inputs 数组来存储每个输入组的数据。在添加输入组时,我们只需要向 inputs 数组中添加一个新的对象即可。

在模板中,我们使用 Element UI 的表单组件来渲染输入框,并使用 v-for 指令循环渲染多组输入框。在每个输入框中,我们使用 v-model 指令将输入值绑定到 inputs 数组中的数据属性上。对于 name 和 age 输入框,我们使用 pattern 规则来进行校验,并在 rules 对象中提供了相应的错误提示信息。在 checkNameInput 和 checkAgeInput 方法中,我们调用 $refs.form.validateField 方法来手动触发校验,并将当前输入对象作为参数传递进去。

最后,我们需要在 Vue 实例中声明一个 form 对象来维护表单数据,并将 rules 对象传递给 el-form 组件的 rules 属性。这样,每次输入框的值发生变化时,就会自动触发 Element UI 的校验机制,并显示相应的错误提示信息。

补充:vue动态表单添加表单验证

<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="运算关系"
                 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="结束日期"
                       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="开始时间"
                       end-placeholder="结束时间"
                       placeholder="选择时间范围"
                       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: '运算关系不能为空', trigger: 'change' }
   ],
   conditionValue: [
     { required: true, message: '值不能为空', trigger: 'change' }
   ],
   conditionTrueValueList: [
     { required: true, message: '值不能为空', trigger: 'change' }
   ]
 },

主要就是prop的问题,prop要对应到数据。

来源:https://www.cnblogs.com/qutao125/archive/2023/04/06/17293924.html

标签:vue,添加,表单,validateField,验证
0
投稿

猜你喜欢

  • numpy matrix和array的乘和加实例

    2022-09-29 15:17:45
  • Python中条件选择和循环语句使用方法介绍

    2023-01-08 23:08:03
  • JSP实现登录功能之添加验证码

    2023-07-16 12:18:09
  • 基于Bootstrap实现下拉菜单项和表单导航条(两个菜单项,一个下拉菜单和登录表单导航条)

    2024-04-16 08:49:09
  • PHP结构型模式之享元模式详解

    2023-05-27 22:38:40
  • python实现的分层随机抽样案例

    2022-06-07 16:10:42
  • Vue中正确使用jQuery的方法

    2023-07-02 17:07:45
  • mysql:Can't start server: can't create PID file: No space left on device

    2024-01-24 23:20:46
  • 经验之谈:MySQL与ASP.NET配合更强大

    2008-12-23 15:26:00
  • Python深度学习之Pytorch初步使用

    2023-08-12 08:16:41
  • Python基础进阶之海量表情包多线程爬虫功能的实现

    2022-03-19 11:52:13
  • Git建立本地仓库并上传到Gitee的详细步骤

    2023-03-06 20:45:21
  • 解决Pycharm界面的子窗口不见了的问题

    2022-03-06 15:21:15
  • Python内存管理器如何实现池化技术

    2023-05-12 15:29:02
  • python 移动图片到另外一个文件夹的实例

    2022-09-17 07:56:14
  • python基本数据类型练习题

    2022-07-21 14:26:26
  • MySQL中LAG()函数和LEAD()函数的使用

    2024-01-27 14:05:35
  • 解决Python传递中文参数的问题

    2021-04-10 09:00:07
  • Python实现网络自动化eNSP

    2021-01-18 00:48:04
  • 数据库连接方式汇总

    2024-01-15 22:15:06
  • asp之家 网络编程 m.aspxhome.com