打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
vue动态创建form表单,如何添加rules验证?
userphoto

2023.10.30 天津

关注

动态生成的表单要用:prop属性,语法:prop="'v-for绑定的数组.’ + index + '.v-model绑定的变量’"。

比如:prop="'optionArray.’ + index +’.prop’",optionArray是v-for绑定的数组,index是索引,prop是表单绑定的v-model的名称,使用.把他们连接起来。

示例代码如下:

<template>
  <div>
    <el-form ref="form" :model="addForm" label-width="80px">
      <div class="moreRules">
        <div 
class="moreRulesIn"
          v-for="(item, index) in addForm.moreNotifyObject"
          :key="item.key">
          <el-row>
            <el-col :span="6">
              <el-form-item
                label="field"
                :prop="'moreNotifyObject.' + index + '.field'"
                :rules="[{ required: true, message: '请完善信息' }]">
                <el-input
                  v-model="item.field"
                  placeholder="请输入field"
                  class="el-select_box"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label="name"
                :prop="'moreNotifyObject.' + index + '.name'"
                :rules="[{ required: true, message: '请完善信息' }]"
              >
                <el-input
                  v-model="item.name"
                  placeholder="请输入name"
                  class="el-select_box"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item
                label="max"
                :prop="'moreNotifyObject.' + index + '.max'"
                :rules="[{ required: true, message: '请完善信息' }]"
              >
                <el-input
                  v-model="item.max"
                  placeholder="请输入max"
                  class="el-select_box"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6" align="center">
              <el-button @click="deleteRules(item, index)">删除</el-button>
            </el-col>
          </el-row>
        </div>
         <el-button @click="addUser">增加</el-button>
         <el-button @click="initData">清空</el-button>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      addForm: {
        name: "",
        content: "",
        moreNotifyObject: [
          {
            field: "",
            name: "",
            max: "",
          },
          {
            field: "",
            name: "",
            max: "",
          },
        ],
      },
    };
  },
  methods: {
    initData() {
      this.addForm.moreNotifyObject = [];
    },
    addUser() {
      this.addForm.moreNotifyObject.push({
        field: "",
        name: "",
        max: "",
      });
    },
    deleteRules(item, index) {
      this.index = this.addForm.moreNotifyObject.indexOf(item);
      if (index !== -1) {
        this.addForm.moreNotifyObject.splice(index, 1);
      }
    },
  },
};
</script>
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
成功使用el-radio-group
vue element table中自定义一些input的验证
vue踩坑:vue+ element ui 表单验证有值但验证失败。
自定义laravel表单请求验证类(FormRequest共用一个rules())
uniapp组件-uni-forms
前端:Element UI 多选框组用法笔记
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服