v-for 循环生成多个表单元素 给动态生成的表单元素绑定值并且添加校验规则

本文介绍如何使用Vue.js实现动态表单,创建下级部门并实时绑定输入值。通过v-for循环和v-model双向数据绑定,同时设置了输入验证规则,确保数据的正确性和结构管理。

 需求:点击新增按钮 能不断生成下级部门,所以我再data中定义了一个变量 空数组

subordinateDepartmentNum:[] 默认值设置为[]

给增加按钮添加点击事件 每点击一次按钮  subordinateDepartmentNum加1

// 新增下级部门
addDepartment() {
 this.subordinateDepartmentNum.push(this.subordinateDepartmentNum.length);
//确保每次新增的下级部门的文本框的内容都是空的--
this.childrenData.deptName[this.subordinateDepartmentNum.length-1] = undefined;
},

通过v-for循环生成表单  这样就可以生成多个下级部门的表单元素

  <div class="add_department_box" v-for="(item,index) in subordinateDepartmentNum" :key="index">
  </div>

那么问题来了 如何绑定 给循环出来的表单绑定值以及添加验证规则呢?

绑定值的方法:

在data中定义一个值childrenData:{ deptName:[] } 通过以下方式绑定值:

 <el-form-item label="名称" class="child_dept_name">
<el-input v-model="childrenData.deptName[index]"></el-input>
</el-form-item>

绑定后 在输入框中输入值,打印childrenData的值 得到一个数组,数组中的值是表单元素中输入的值

 添加校验规则的方法: 在表单中添加:prop 和 :rules 我是循环的数字 n是从1开始的

<el-form ref="childForm" :model="childrenData" :rules="childrenRules" class="add_department_form"
         label-width="80">
  <div class="add_department_box" v-for="(item,index) in subordinateDepartmentNum" :key="index">
    <el-form-item label="名称" :prop="'deptName.'+index" :rules="childrenRules.deptName" class="child_dept_name">
      <el-input v-model="childrenData.deptName[index]"></el-input>
    </el-form-item>
  </div>
</el-form>
//新增的下级架构校验规则
childrenRules: {
  deptName: [
    {required: true, message: "名称不能为空", trigger: "blur"}
  ]
},

完整的代码

 <el-form ref="childForm" :model="childrenData" :rules="childrenRules" class="add_department_form"
               label-width="80">
        <div class="add_department_box" v-for="(item,index) in subordinateDepartmentNum" :key="index">
          <el-form-item label="" prop="" class="child_num">
            <div class="num_text">{{ index + 1 }}</div>
          </el-form-item>
          <el-form-item label="名称" :prop="'deptName.'+index" :rules="childrenRules.deptName" class="child_dept_name">
            <el-input v-model="childrenData.deptName[index]"></el-input>
          </el-form-item>
          <el-form-item label="" prop="" class="child_type">
            <el-radio-group v-model="childrenData.type">
              <el-radio :label="1">公司</el-radio>
              <el-radio :label="2">部门</el-radio>
            </el-radio-group>
          </el-form-item>
          <div class="close" @click="closeDepartmentBox(index)">
            <span class="iconfont icon-31guanbi"></span>
          </div>
        </div>
      </el-form>



 data() {
    return {
      //新增的下级架构数据
      //新增的下级架构数据
      childrenData: {
        deptName: [],
        type: 2
      },
      //新增的下级架构校验规则
      childrenRules: {
        deptName: [
          {required: true, message: "名称不能为空", trigger: "blur"}
        ]
      },
      // 新增的下级部门的数量
      subordinateDepartmentNum: [],
    }
  },

在 Vue3 中使用 v-for 循环渲染多个组件校验组件表单,可结合多个方法实现。 首先,为每个子组件设置动态 ref,方便后续访问特定子组件实例。如以下代码所示: ```vue <template> <div> <ChildComponent v-for="(item, index) in childList" :key="index" :ref="(el) => childRefs[index] = el" /> </div> </template> <script setup> import { ref } from 'vue'; import ChildComponent from './ChildComponent.vue'; const childList = ref([/* 子组件数据 */]); const childRefs = ref([]); </script> ``` 这里通过 `:ref="(el) => childRefs[index] = el"` 为每个子组件绑定了动态 ref,存储在 `childRefs` 数组中。 接着,在父组件里编写校验函数,遍历子组件的 ref 数组,调用子组件校验方法。示例如下: ```vue <template> <div> <ChildComponent v-for="(item, index) in childList" :key="index" :ref="(el) => childRefs[index] = el" /> <button @click="validateAllForms">校验所有表单</button> </div> </template> <script setup> import { ref } from 'vue'; import ChildComponent from './ChildComponent.vue'; const childList = ref([/* 子组件数据 */]); const childRefs = ref([]); const validateAllForms = async () => { let allValid = true; for (const ref of childRefs.value) { if (ref) { const valid = await ref.validateForm(); if (!valid) { allValid = false; } } } if (allValid) { // 所有表单校验通过,执行相应操作 } else { // 存在表单校验不通过的情况 } }; </script> ``` 在 `validateAllForms` 函数中,遍历 `childRefs` 数组,调用每个子组件的 `validateForm` 方法进行校验,根据返回结果判断所有表单是否都通过校验。 在子组件里,需要实现 `validateForm` 方法,进行具体的表单校验。示例如下: ```vue <template> <el-form ref="formRef" :model="formData" :rules="rules"> <el-form-item prop="field1"> <el-input v-model="formData.field1" /> </el-form-item> </el-form> </template> <script setup> import { ref } from 'vue'; import { ElForm } from 'element-plus'; const formRef = ref<InstanceType<typeof ElForm>>(); const formData = ref({ field1: '' }); const rules = ref({ field1: [ { required: true, message: '该字段为必填项', trigger: 'blur' }, ], }); const validateForm = () => { return new Promise((resolve) => { formRef.value?.validate((valid) => { resolve(valid); }); }); }; </script> ``` 子组件中的 `validateForm` 方法调用 `el-form` 的 `validate` 方法进行表单校验,并返回一个 Promise,方便父组件获取校验结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值