el-from中校验,如果某一项需要另一项填写才能校验

使用validateField

<el-form
      :model="params"
      :rules="rules"
      :scroll-to-error="true"
      ref="refrom"
      v-else
      >
        <el-form-item label="用户姓名" prop="name">
          <el-input placeholder="请输入用户姓名" v-model="params.name"></el-input>
        </el-form-item>
        <el-form-item label="证件类型" prop="certificatesType">
          <el-select
            style="width: 100%"
            placeholder="请选择证件类型"
      v-model="params.certificatesType"
          >
          <el-option
      v-for="(item,index) in arrType"
      :key="index"
      :label="item.name"
      :value="item.value"
      
    />
          </el-select>
        </el-form-item>
        <el-form-item label="证件号码" prop="certificatesNo" >
          <el-input placeholder="请输入证件号码" v-model="params.certificatesNo"></el-input>
        </el-form-item>
        <el-form-item label="上传证件" prop="certificatesUrl">
          <!-- action:upload组件向服务器提交图片路径
               limit:照片墙约束图片个数
               on-exceed:超出约束数量的钩子
          -->
          <el-upload
          ref="upload"
          v-model="params.certificatesUrl"
            action="/api/oss/file/fileUpload?fileHost=userAuah"
            limit="1"
            list-type="picture-card"
            :on-exceed="handleExceed"
            :on-success="handlesuccess"
            :on-remove = 'handleremove'
            :on-preview="handlePictureCardPreview"
  >
  <img
            style="width: 100%; height: 100%"
            src="../../../assets/images!auth_example.png"
            alt=""
          />

  </el-upload>

  <el-dialog v-model="dialogVisible">
    <img w-full alt="Preview Image" :src="params.certificatesUrl" v-if="params.certificatesUrl"/>
  </el-dialog>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" size="default" @click="submit">提交</el-button>
          <el-button type="primary" size="default" @click="reset">重写</el-button>
        </el-form-item>
      </el-form>

1,先给form表单绑定ref 
 //创建form表单的实例
 const refrom = ref()

2,给表单绑定rules校验
 const rules = {
    name: [{ validator: checkName, trigger: 'change',required: true }],
    certificatesType: [{ validator: checkcertificatesType,required: true }],
    certificatesUrl: [{ validator: checkcertificatesUrl, trigger: 'change',required: true }],
    certificatesNo: [{ validator: checkcertificatesNo, trigger: 'change' ,required: true }]
 }

const checkcertificatesNo = (rule: any, value: any, callback: any) =>{
    if(params.certificatesType == ''){
        refrom.value.validateField('certificatesType',(errorMessage:any)=>{
            console.log(errorMessage,'errorMessage')
        })
    }
    console.log(params.certificatesType,'params.certificatesType')
 }

3,开始自定义校验

  //自定义name校验
  const checkName = (rule: any, value: any, callback: any) =>{
    const res = /^[\u4e00-\u9fa5]+$/
    if(res.test(value)){
        callback()
    }else{
        callback('请输入正确的名字')
    }
 }
 //自定义证件类型校验
 const checkcertificatesType = (rule: any, value: any, callback: any) =>{
    if(value == 10 || value == 20){
        callback()
    }else{
        callback('请选择证件类型')
    }
    console.log(value,'value')
 }
//自定义校验图片
//  const checkcertificatesUrl = (rule: any, value: any, callback: any) =>{
//     console.log(params.certificatesType,'params.certificatesType')
//  }
 //自定义校验证件号码
 const checkcertificatesNo = (rule: any, value: any, callback: any) =>{

//关键代码 validateField ,是一个函数,第一个填写要校验的某一项,第二个是一个函数,返回true和false,会自动触发certificatesType的校验规则,只需要在里面判断逻辑即可
       refrom.value.validateField('certificatesType',(errorMessage:any)=>{
            if(errorMessage == true){
                if(params.certificatesType == 10){
                    const reg = /^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{4}$/
                    if(reg.test(value)){
                        callback() 
                    }else{
                        callback('请输入正确的身份证')
                    }
                }else{
                    let hkb = /^\d{9}$/;
                    if(hkb.test(value)){
                        callback() 
                    }else{
                        callback('请输入正确的户口本')
                    }
                }
            }
        })

    console.log(params.certificatesType,'params.certificatesType')
 }
相关推荐
cs_dn_Jie3 小时前
钉钉 H5 微应用 手机端调试
前端·javascript·vue.js·vue·钉钉
开心工作室_kaic4 小时前
ssm068海鲜自助餐厅系统+vue(论文+源码)_kaic
前端·javascript·vue.js
有梦想的刺儿4 小时前
webWorker基本用法
前端·javascript·vue.js
customer085 小时前
【开源免费】基于SpringBoot+Vue.JS周边产品销售网站(JAVA毕业设计)
java·vue.js·spring boot·后端·spring cloud·java-ee·开源
清灵xmf5 小时前
TypeScript 类型进阶指南
javascript·typescript·泛型·t·infer
小白学大数据5 小时前
JavaScript重定向对网络爬虫的影响及处理
开发语言·javascript·数据库·爬虫
qq_390161775 小时前
防抖函数--应用场景及示例
前端·javascript
334554326 小时前
element动态表头合并表格
开发语言·javascript·ecmascript
John.liu_Test6 小时前
js下载excel示例demo
前端·javascript·excel
PleaSure乐事6 小时前
【React.js】AntDesignPro左侧菜单栏栏目名称不显示的解决方案
前端·javascript·react.js·前端框架·webstorm·antdesignpro