element循环表单验证(判断数据是否重复)

碰到一个需求,table里面的数据可以添加国家,国家不能重复,大概成果如下。

这是一个循环嵌套的list,琢磨了会才做出来。

模板部分:

   <el-form
    ref="form"
    :model="form"
    :inline-message="true"
    label-position="left"
    label-width="120px"
    :rules="rules"
    :inline="true">
    <el-table :data="form.quarantineProductDeclarationList" border style="width: 100%">
            <el-table-column label="序号" :resizable="false" width="50px" type="index" align="center"></el-table-column>

            ......中间字段省略

            <el-table-column label="来自国家和地区数量" :resizable="false" align="center">
              <template slot-scope="scope" v-if="scope.row.isSituation">
                <div v-for="(item, index) in scope.row.detailList" style="margin: 2px 0; display: flex; align-items: center">
                  <el-form-item label="" :prop="`quarantineProductDeclarationList[${scope.$index}].detailList[${index}].countryRegion`"
                    :rules="rules.quarantineProductDeclarationList.detailList.countryRegion">
                    <back-dict-select dict="国家地区" class="formInput" v-model="item.countryRegion" placeholder="请选择国家" size="mini" :disabled="!isEdit"/>
                  </el-form-item>
                  <el-input-number class="tableInput" v-model="item.num" :min="0" size="mini" :disabled="!isEdit"></el-input-number>
                  <div style="width: 150px; display: flex; flex-shrink: 0">
                    <el-button v-if="index === scope.row.detailList.length - 1" @click="addDetailList(scope.row.detailList)" size="mini" :disabled="!isEdit">添加一行</el-button>
                    <el-button @click="delDetailList(scope.row.detailList, index)" size="mini" type="danger" plain :disabled="!isEdit">删除</el-button>
                  </div>
                </div>
              </template>
            </el-table-column>
          </el-table>
  </el-form>

js部分:
 

// 获取quarantineProductDeclarationList及内部数据的工具函数
function getObjectByPath(obj, path, depth = 2) {
    let truncatedPath = path
        .replace(/\[(\d+)\]/g, '.$1')  // 将数组索引 '[0]' 形式转换为 '.0'
        .split('.')
        .slice(0, depth + 1)            // 截取到指定层级(+1 是为了包含数组索引部分)
        .join('.');                     // 将路径重新拼接成字符串
    return truncatedPath
        .split('.')
        .reduce((acc, key) => acc && acc[key], obj);
  }
// 判断有没有重复字段
 function hasDuplicateValueByKey(arr, key) {
    const seen = {};
    for (const item of arr) {
      // 假设每个对象都有这个key
      if (item.hasOwnProperty(key)) {
        const value = item[key];
        if (seen[value]) {
          // 如果这个值已经被记录在seen中,说明有重复
          return true;
        }
        seen[value] = true; // 否则,记录这个值
      }
    }
    // 如果没有找到重复的值,返回false
    return false;
  }

data () {
  return {
        form: {
            quarantineProductDeclarationList: [],
        },
        rules: {
            quarantineProductDeclarationList: {
            detailList: {
              countryRegion: [{ required: true, message: '请选择计划排放日期', trigger: 'blur' }, { validator: this.checkoutType }]
            }
          },
        }
    }
},
methods: {
     checkoutType (rule, value, callback) {
        // 先拿到detailList
        const detailList = getObjectByPath(this.form, rule.field) || []
        // 判断detailList里的countryRegion是否重复
        if (hasDuplicateValueByKey(detailList, 'countryRegion')) {
          callback("国家出现重复")
        }
      },
}

Vue 是一个流行的 JavaScript 框架,用于构建用户界面和单页面应用程序。Element UI 是一个基于 Vue.js 的组件库,提供了丰富的 UI 组件和工具。在 Vue 中使用 Element UI 构建表单,可以通过循环来动态生成表单项并进行验证。 首先,我们可以使用 Vue 的 v-for 指令来循环渲染表单项。通过遍历数据源(如数组或对象)来动态生成表单字段,例如输入框、下拉框、复选框等。这样就可以实现根据数据源的变化而动态生成不同数量的表单项,方便灵活地管理表单内容。 其次,通过 Element UI 提供的验证规则和验证器,可以实现对表单循环验证。我们可以为每个表单项设置相应的验证规则(如必填、格式验证等),并通过验证器对整个表单进行验证。在循环中,可以动态添加、修改或删除验证规则,以实现对动态生成的表单项的实时验证。 另外,可以利用 Element UI 提供的表单组件和事件处理机制来实现更进一步的表单循环验证。例如,可以监听表单项的输入变化、聚焦和失焦事件,在相应的事件处理函数中进行数据的验证和反馈。这样就可以及时提示用户输入的正确与否,提升用户体验。 总之,使用 Vue 和 Element UI 可以很容易地实现表单循环验证。通过动态生成表单项、设置验证规则和事件处理,可以灵活地管理和验证表单数据,提供更好的用户交互体验。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值