如何动态添加表单及校验

ui : 饿了么
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

需求 : 点击新增可以添加表单

// 页面代码 
<el-form ref="dynamicValidateForm" :model="dynamicValidateForm" label-width="100px" class="demo-dynamic">
 <a-card v-for="(item, index) in form1.dynamicItem" :key="index">
          <a-row>
            <a-col :span="12">
              <el-form>
                <el-form-item
                  label="标题"
                  :prop="`dynamicItem.`+index+`.name`'"             // 这里的自定义prop必须按照文档写, 不然无法校验
                  :rules="{
                    required: true, message: '标题不能为空', trigger: 'blur'
                  }"
                >
                  <el-input v-model="item.name" style="width:300px" />
                </el-form-item>
                <el-form-item
                  label="摘要"
                  :prop="`dynamicItem.`+index+`.phone`'"  
                  :rules="[
                    {required: true, message: '摘要不能为空', trigger: 'blur'},
                  ]"
                >
                  <el-input v-model="item.phone" style="width:300px" />
                </el-form-item>
                <el-form-item label="页面背景图" :prop="`dynamicItem.`+index+`.imagUrl`'"  >
                  <el-upload
                    action="/"
                    class="avatar-uploader"
                    :data="{index}"
                    :http-request="Upload"
                    :show-file-list="false"
                    accept=".jpg,.jpeg,.png,.JPG,.JPEG"
                  >
                    <el-image v-show="item.imageUrl" style="width: 200px;height: 200px" :src="item.imageUrl" />
                    <i v-show="!item.imageUrl" class="el-icon-plus avatar-uploader-icon" />
                  </el-upload>
                </el-form-item>
                <el-form-item>
                  <i class="el-icon-delete" @click="deleteItem(item, index)" />
                </el-form-item>
              </el-form>
            </a-col>
            <a-col :span="12">
              <div class="market">
                <div class="market_left">
                  <p style="font-size: 24px;font-weight: 500">{{ item.name }}</p>
                  <p style="font-size: 16px">{{ item.phone }}</p>
                </div>
                <div class="market_right" />
              </div>

            </a-col>
          </a-row></a-card>
          // 触发事件
           <el-button @click="addDomain">新增标摘封</el-button>
             <el-button
              style="color: #FFFFFF;
                  background-color: #cf001a;
                   border-color: #cf001a;"
              @click="submitForm('dynamicValidateForm')"> 提 交
            </el-button>
 </el-form>   
 // data内容
 data() {
    return {
      form1: {
        name: '',
        phone: '',
        imageUrl: '',
        dynamicItem: [{ imageUrl: '',
          name: '',
          phone: '' }]
      }
      }}
// 方法 
// 提交表单 
 submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.commitInfo() // 提交表单
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },

// 删除方法 
deleteItem(item, index) {
  this.form1.dynamicItem.splice(index, 1)
},
// 动态添加表单的方法 
    addDomain() {
      if (this.form1.dynamicItem.length < '3') {
      // 最多只能添加三条
        this.form1.dynamicItem.push({
          name: '',
          phone: '',
          imageUrl: ''
        })
      } else {
        this.$message.warning('最多只能添加三个!')
      }
      console.log(111, this.form1.dynamicItem)
    },
    // 自定义图片上传的方法 
   Upload(file) {
      // console.log(file, item, 88676765786)
      // return
      var that = this
      // setTimeout(function() {
      async function multipartUpload() {
        console.log(file)
        const temporary = file.file.name.lastIndexOf('.')
        console.log(temporary, 22)
        const fileNameLength = file.file.name.length
        const fileFormat = file.file.name.substring(
          temporary + 1,
          fileNameLength
        )
        // // 文件名字
        const fileName = new Date().getTime() + '.' + fileFormat
        // 以下内容需要根据具体情况配置   需要初始化oss内容
        const ossInfo = that.ossInfo
        const OSS = require('ali-oss')
        const store = new OSS({
          stsToken: ossInfo.SecurityToken,
          endpoint: ossInfo.endpoint,
          accessKeyId: ossInfo.AccessKeyId,
          accessKeySecret: ossInfo.AccessKeySecret,
          bucket: ossInfo.bucket
        })
        const options = {
          callback: {
            url: ossInfo.callbackUrl,
            body: ossInfo.callbackBody,
            contentType: ossInfo.callbackBodyType
          }
        }
        const res = await store.put(fileName, file.file, options)
        if (res.data.error_code === 200) {
        //  成功拿到回调
        // 回调的图片地址复制给上方的动态图片地址。
          that.form1.dynamicItem[file.data.index].imageUrl = res.url
          console.log(this, that, 8888)
        } else {
          this.$message.error(res.message)
        }
      }
       multipartUpload()
    },
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue 是一个非常受欢迎的 UI 组件库,它提供了丰富的单组件,包括输入框、下拉选择框等等。要实现动态添加单必填校验,可以按照以下步骤进行操作: 1. 首先,引入 Ant Design Vue 的 Form 组件和相应的验证规则模块,例如 `required` 必填规则。 2. 在 Vue 的 data 中定义一个单对象,包含需要动态添加单控件的值。 3. 在模板中使用 Ant Design Vue 的 Form 组件,并使用 v-model 指令将单对象和控件的值进行绑定。 4. 使用 v-for 指令遍历需要动态添加单控件,通过一个数组来提供控件的模板。 5. 在单控件的模板中,使用 :rules 属性绑定验证规则,例如 `:rules="[{ required: true, message: '该字段为必填项' }]` 6. 在模板中提供一个按钮或其他交互方式,用来触发动态添加单控件的操作。 7. 当点击按钮时,通过修改单对象的属性或者添加新的属性,实现动态添加单控件。 8. 提交单时,通过调用 validate 方法对单进行校验,这会触发相关的验证规则。 9. 根据校验结果,可以在模板中展示错误信息或者处理其他逻辑。 需要注意的是,动态添加单必填校验时,需要在动态添加单控件的同时,将相应的验证规则也添加单对象中。另外,为了方便校验和错误信息的展示,建议使用对应的验证规则模块和提示文本。 这样,通过上述步骤,就可以实现 Ant Design Vue 动态添加单必填校验了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值