vue form表单循环校验

<template>
  <div class="box">
    <el-form :model="item" :rules="rules" :ref="`formRef_${index}`" v-for="(item,index) in ruleForm" :key="index">
      <div class="content">
        <Title :title="`机构${index+1}`" />
        <el-form-item label="机构名称" prop="name">
          <el-input v-model="item.name" class="w-340" maxlength="50"></el-input>
        </el-form-item>
        <el-form-item label="机构属性" class="checkbox-group" prop="type">
          <el-checkbox-group v-model="item.type">
            <el-checkbox label="管理中心" name="1"></el-checkbox>
            <el-checkbox label="推广中心" name="2"></el-checkbox>
          </el-checkbox-group>
        </el-form-item>
      </div>
      <div class="btn">
        <el-button @click="del(index)" v-if="ruleForm.length >1">删除</el-button>
        <el-button @click="add">添加</el-button>
      </div>
    </el-form>

    <el-button style="margin-top:100px" type="primary" @click="submitForm">提交</el-button>
  </div>
</template>

<script>
export default {
  data () {
    return {
      rules: {
        name: [
          { required: true, message: '请输入机构名称', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择机构属性', trigger: 'change' }
        ]
      },
      ruleForm: [
        {
          name: '',
          type: []
        }
      ]
    }
  },
  methods: {
    del (index) {
      this.ruleForm.splice(index, 1)
    },
    add () {
      this.ruleForm.push(
        {
          name: '',
          type: []
        }
      )
    },
    // 提交
    submitForm () {
     const arr = []
      this.ruleForm.forEach(async (item, index) => {
        try {
          await this.$refs[`formRef_${index}`][0].validate()
          arr.push(true)
        } catch (error) {
          arr.push(false)
        }
        const flag = arr.every(item => item)
        console.log(flag)
      })
    }
  }
}
</script>

<style lang="less" scoped>
.box {
  margin: 300px;
}
</style>

相关推荐

  1. vue3+element-plus 校验循环form校验

    2024-01-25 08:48:01       26 阅读
  2. el-form v-for循环列表的如何校验

    2024-01-25 08:48:01       41 阅读
  3. element-plus中的校验

    2024-01-25 08:48:01       47 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-25 08:48:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-25 08:48:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-25 08:48:01       82 阅读
  4. Python语言-面向对象

    2024-01-25 08:48:01       91 阅读

热门阅读

  1. stm32 - 工程配置

    2024-01-25 08:48:01       48 阅读
  2. wpf UI虚拟化

    2024-01-25 08:48:01       56 阅读
  3. 贪心算法理论基础

    2024-01-25 08:48:01       52 阅读
  4. Tensorflow 中的损失函数 —— loss 专题汇总

    2024-01-25 08:48:01       56 阅读
  5. DALL·E与NFT:开启艺术数字化的新篇章

    2024-01-25 08:48:01       59 阅读
  6. MYSQL

    MYSQL

    2024-01-25 08:48:01      52 阅读
  7. 书籍 - 华杉讲透孙子兵法 - 8

    2024-01-25 08:48:01       41 阅读
  8. 递归函数的介绍和实现

    2024-01-25 08:48:01       57 阅读
  9. Linux平台下安全编译

    2024-01-25 08:48:01       57 阅读
  10. Sql server强制走索引

    2024-01-25 08:48:01       56 阅读
  11. LayUI 监听 Radio

    2024-01-25 08:48:01       46 阅读
  12. js ts函数重载

    2024-01-25 08:48:01       44 阅读