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>
相关推荐
EndingCoder6 小时前
Electron 跨平台兼容性:处理 OS 差异
前端·javascript·electron·前端框架·node.js·chrome devtools
zhong liu bin6 小时前
Vue框架技术详解——项目驱动概念理解【前端】【Vue】
前端·javascript·vue.js·vscode·vue
前端 贾公子6 小时前
ElementUI 中 validateField 对部分表单字段数组进行校验时多次回调问题
前端·javascript·elementui
棒棒的唐6 小时前
vue2 elementUI 登录页面实现回车提交登录的方法
前端·javascript·elementui
zhangzuying10266 小时前
基于Vue3 +ElementuiPlus + Dexie.js自研的浏览器插件新建标签页tab
vue.js·typescript·echarts
lichong9516 小时前
【混合开发】vue+Android、iPhone、鸿蒙、win、macOS、Linux之video 的各种状态和生命周期调用说明
android·vue.js·macos
知识分享小能手6 小时前
React学习教程,从入门到精通,React 使用属性(Props)创建组件语法知识点与案例详解(15)
前端·javascript·vue.js·学习·react.js·前端框架·vue
摸鱼的春哥7 小时前
前端程序员最讨厌的10件事
前端·javascript·后端
牧羊狼的狼11 小时前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手13 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react