vue form表单循环校验

javascript 复制代码
<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>
相关推荐
清晨Coding13 分钟前
删了 Cookie 还能自动登录?Google系网站登录态自动恢复的原理分析
javascript
kyriewen24 分钟前
我扫了 814 个依赖,97 个还敢要 Node 10
前端·javascript·程序员
程序员大阳2 小时前
HTML+CSS+JS对话式实战(9)-项目总结与问题分析
javascript·css·html·实战·项目总结
Hilaku3 小时前
MCP 协议会成为前端 AI 工程的标准接口吗?
前端·javascript·程序员
RobinDevNotes5 小时前
从 Electron 到 GPUI,9 个跨平台桌面方案该怎么选
前端·javascript·rust·electron·openresty·前端开发工具
软件技术NINI6 小时前
基于SpringBoot和Vue的新能源汽车销售推荐系统
java·vue.js·spring boot·后端·汽车·毕业设计·毕设
程序员大阳6 小时前
HTML+CSS+JS对话式实战(6)-学生端查看活动功能开发
javascript·css·html·模块·查看学生
招风的黑耳7 小时前
SpringBoot+Vue+MySQL 民宿在线预定平台 完整可运行版(源码+文档)
vue.js·spring boot·mysql
子兮曰1 天前
Chrome 权限元素实战:144 与 151 的渐进增强写法
前端·javascript·chrome
强尼银手1 天前
我花一天做了个格斗游戏原型,判定全对,然后被「美术」劝退了
前端·javascript