element form表单动态添加新增一行加校验

<el-form
  :model="noticForm"
   ref="noticForm"
   label-width="70px"
   :inline="true"
 >
   <el-row>
     <el-col :span="10">
       <el-form-item
         v-for="(nameItem, index) in noticForm.noticConfig.name"
         :key="nameItem.key"
         :prop="'noticConfig.name.' + index + '.value'"
         label="姓名:"
         :rules="rules.name"	
       >
         <el-input v-model="nameItem.value"></el-input>
       </el-form-item>
     </el-col>
     <el-col :span="10">
       <el-form-item
         prop="phone"
         label="电话:"
         v-for="(phoneItem, index) in noticForm.noticConfig.phone"
         :key="phoneItem.key"
         :prop="'noticConfig.phone.' + index + '.value'"
         :rules="rules.phone"
       >
         <el-input v-model="phoneItem.value"></el-input>
       </el-form-item>
     </el-col>
<el-col :span="6">
            <el-form-item
              prop="hospitalId"
              label="医院:"
              v-for="(hospitalIdItem, index) in noticForm.hospitalId"
              :key="hospitalIdItem.key"
              :prop="'hospitalId.' + index + '.value'"
              :class="{ active: !hospitalIdItem.show }"
              // 动态校验
              :rules="[{ required: hospitalIdItem.show, message: '请选择医院', trigger: 'change' }]"
            >
              <el-select
                v-model="hospitalIdItem.value"
                placeholder="请选择医院"
              >
                <el-option
                  v-for="(item, index) in hospitalRange"
                  :key="index"
                  :label="item.hospitalName"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
   </el-row>
   <el-form-item style="width: 100%; text-align: center">
     <el-button type="primary" @click="submitForm('noticForm')"
       >提交</el-button
     >
     <el-button @click="addDomain">新增</el-button>
     <el-button @click.prevent="removeDomain">删除</el-button>
   </el-form-item>
 </el-form>


//data
noticForm: {
  noticConfig: {
     name: [
       {
         value: "",
       },
     ],
     phone: [
       {
         value: "",
       },
     ],
   },
 },
 rules: {
        phone: [{ required: true, message: "必填", trigger: "blur" }],
        name: [{ required: true, message: "必填", trigger: "blur" }],
      },

// 删除
removeDomain() {
 // console.log(this.noticForm.noticConfig.phone);
  // console.log(this.noticForm.noticConfig.name);
  if (this.noticForm.noticConfig.name.length > 1) {
    this.noticForm.noticConfig.phone.splice(-1, 1);
    this.noticForm.noticConfig.name.splice(-1, 1);
  } else {
    this.$message({
      message: "已经是最后一项了,不能删除了",
      type: "error",
    });
  }
},
// 表单新增
addDomain() {
  this.noticForm.noticConfig.name.push({
    value: "",
    key: Date.now(),
  });
  this.noticForm.noticConfig.phone.push({
    value: "",
    key: Date.now(),
  });
},
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以通过以下步骤实现vue3 element form动态表单添加删除和校验: 1. 创建一个数组来存储表单项的数据,例如: ``` data() { return { formItems: [ { label: '姓名', prop: 'name', value: '', rules: [{ required: true, message: '请输入姓名' }] }, { label: '年龄', prop: 'age', value: '', rules: [{ required: true, message: '请输入年龄' }, { type: 'number', message: '年龄必须为数字' }] } ] } } ``` 2. 在模板中使用`el-form`和`el-form-item`标签来渲染表单,例如: ``` <el-form :model="formData" :rules="formRules" ref="form"> <el-form-item v-for="(item, index) in formItems" :key="index" :label="item.label" :prop="item.prop"> <el-input v-model="item.value"></el-input> <el-button type="danger" size="small" icon="el-icon-close" @click="removeFormItem(index)" v-if="formItems.length > 1"></el-button> </el-form-item> <el-button type="primary" @click="addFormItem">添加表单项</el-button> <el-button type="primary" @click="submitForm">提交</el-button> </el-form> ``` 3. 在方法中实现添加和删除表单项的功能,例如: ``` methods: { addFormItem() { this.formItems.push({ label: '新表单项', prop: 'new_prop', value: '', rules: [{ required: true, message: '请输入新表单项' }] }) }, removeFormItem(index) { this.formItems.splice(index, 1) }, submitForm() { this.$refs.form.validate((valid) => { if (valid) { // 表单校验通过,提交表单数据 } else { // 表单校验不通过 } }) } } ``` 4. 在表单校验规则中使用动态绑定的方式,例如: ``` data() { return { formRules: {} } }, watch: { formItems: { handler(newValue) { this.formRules = {} newValue.forEach(item => { this.$set(this.formRules, item.prop, item.rules) }) }, deep: true } } ``` 这样就可以实现vue3 element form动态表单添加删除和校验了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值