element 组件upload form 验证

满勇军
2023-12-01
<el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="80px" style="width:600px">

<el-form-item label="文件" ref="excel" prop="file" >

<el-upload

:action="uploadUrl"

:limit="1"

name="excel"

:multiple="false"

:on-success="onUploadSuccess"

:before-upload="handleBeforeUpload"

:on-error="onUploadError"

:auto-upload="false"

:show-file-list="true"

ref="upload"

:data="ruleForm"

:on-change="chUploadFile"

:before-remove="rmUploadFile"

>

<el-button >选取excel文件</el-button>

</el-upload>

</el-form-item>

</el-form>

data () 

ruleForm: {
        vendorId: '',
        currency: '',
        file: null,
      },

rules: {

vendorId: [{ required: true, trigger: 'change' }],

currency: [{ required: true, trigger: 'change' }],

file: [{ required: true, trigger: 'change' }],

},

on-change 和  before-remove  方法

chUploadFile() {
      this.ruleForm.file = true
      this.$refs.excel.clearValidate()
},

rmUploadFile() {
      this.ruleForm.file = null
}

最后点击上传验证

this.$refs.ruleForm.validate(valid => {
    if (valid) {
            this.$refs.upload.submit()
          } else {
            return false;
          }
)

 

 

 

 类似资料: