You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Vuetify表单动态验证规则失效问题求助

Vuetify动态表单验证问题修复方案

问题核心

表单提交时无刷新验证不生效,且下拉选择框(v-select)与文件输入框(v-file-input)的联动规则(选择Revised Certification或Addendum时限制单PDF上传+对应验证)未按预期执行。

问题诊断

  1. 验证规则逻辑错误:当multiple=false时,v-file-input的绑定值是单个File对象而非数组,原规则中v.isArray的判断完全无效;同时未处理切换选项后文件模型残留旧值的干扰。
  2. 动态规则适配不足:虽然用三元表达式切换规则,但规则本身未匹配v-file-input在不同multiple状态下的绑定值类型。

修正代码

1. 修正验证规则

// 单个PDF文件验证规则(对应Revised Certification/Addendum)
const certificationOrAddendumRule = [
  (v) => !!v || "文件不能为空!",
  (v) => v?.type === 'application/pdf' || "必须上传PDF格式文件!"
]

// 多文件验证规则(其他选项)
const arrayRequiredRule = [
  (v) => (v?.length ?? 0) > 0 || "请选择至少一个文件!"
]

2. 优化表单HTML绑定

<v-form ref="uploadForm" v-model="uploadDocumentValidation" @submit.prevent>
  <v-card-text>
    <div class="text-medium-emphasis mb-4">
      此操作将文件上传至ARMs系统。
    </div>

    <v-select
      v-model="uploadTypeModel"
      :items="cleanAllowableUploadTypes"
      item-title="name"
      label="文件分类"
      :counter="300"
      class="mb-2 mt-5"
      variant="outlined"
      validate-on="submit"
      return-object
      :rules="objectRequiredRule"
    ></v-select>

    <v-file-input
      v-model="filesToUploadModel"
      :show-size="1000"
      color="primary"
      label="文件"
      placeholder="选择文件"
      prepend-inner-icon="mdi-file"
      prepend-icon=""
      variant="outlined"
      validate-on="submit"
      counter
      comfortable
      :multiple="!(uploadTypeModel?.name === 'Revised Certification' || uploadTypeModel?.name === 'Addendum')"
      :rules="(uploadTypeModel?.name === 'Revised Certification' || uploadTypeModel?.name === 'Addendum') ? certificationOrAddendumRule : arrayRequiredRule"
      :accept="(uploadTypeModel?.name === 'Revised Certification' || uploadTypeModel?.name === 'Addendum') ? '.pdf' : ''"
    >
      <template v-slot:selection="{ fileNames }">
        <template v-for="(fileName) in fileNames" :key="fileName">
          <v-chip
            class="me-2"
            color="blue-accent-4"
            size="small"
            label
          >
            {{ fileName }}
          </v-chip>
        </template>
      </template>
    </v-file-input>

    <div class="text-overline mb-2">帮助信息</div>
    <div class="text-medium-emphasis mb-1">
      所有上传文件将存储在云端,每个版本均有冗余备份。
    </div>
  </v-card-text>

  <v-divider class="mt-2"></v-divider>

  <v-card-actions class="my-2 d-flex justify-end">
    <v-btn
      class="text-none px-5"
      rounded="xl"
      text="取消"
      @click="isActive.value = false"
    ></v-btn>

    <v-btn
      class="text-none mr-3 my-3 px-5"
      color="primary"
      rounded="xl"
      text="上传"
      variant="flat"
      @click="uploadFiles()"
      type="submit"
    ></v-btn>
  </v-card-actions>                        
</v-form>

3. 添加类型切换监听(脚本部分)

在组件脚本中添加监听,切换文件类型时自动清空已选文件:

watch(uploadTypeModel, () => {
  filesToUploadModel.value = null // 清空文件输入框,避免旧值干扰新规则
})

4. 保留原上传函数逻辑

const uploadFiles = () => {
  uploadForm.value.validate().then(async (result) => {
    if(result.valid) {
      if(uploadDocumentValidation.value && certificationReviewModel.value)
      {
        loadingFileUpload.value = true
        switch (uploadTypeModel.value.name) {
          case 'Revised Certification':
            await documentStore.uploadRevisedCertification(certificationReviewModel.value.mmcid, filesToUploadModel.value)
            break;
          case 'Addendum':
            await documentStore.uploadAddendum(certificationReviewModel.value.mmcid, filesToUploadModel.value)
            break;
          case 'Supporting Document':
            await documentStore.uploadSupportingDocuments(certificationReviewModel.value.mmcid, filesToUploadModel.value)
            break;
          case 'Contract':
            await documentStore.uploadContractDocuments(certificationReviewModel.value.mmcid, filesToUploadModel.value)
            break;
          case 'ILOS Document':
            await documentStore.uploadIlosDocuments(certificationReviewModel.value.mmcid, filesToUploadModel.value)
            break;
          case 'Miscellaneous':
            await documentStore.uploadMiscellaneousDocuments(certificationReviewModel.value.mmcid, filesToUploadModel.value)
            break;
          case 'DMCP Working File':
            await documentStore.uploadDMCPWorkingDocuments(certificationReviewModel.value.mmcid, filesToUploadModel.value)
            break;
          default:
            console.log('错误:未知文件类型')
        }
        await certificationReviewStore.loadCertificationReview(certificationReviewModel.value.mmcid)
        loadingFileUpload.value = false    
      }  
    }
  })
}

修复说明

  • 移除无效的isArray判断,适配单个File对象的验证逻辑;
  • 使用可选链?.简化表达式,避免空值报错;
  • 添加类型切换监听,自动清空已选文件,防止旧值干扰新验证规则;
  • 保持无刷新验证逻辑,通过uploadForm.value.validate()触发验证,验证通过后执行上传操作。

内容的提问来源于stack exchange,提问作者Qiuzman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 13:44:57