使用 Vuelidate 进行条件验证? [英] Conditional validation with Vuelidate?

查看:29
本文介绍了使用 Vuelidate 进行条件验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,可以根据参数 action 应用不同的验证,存储在 VUEX 商店中.我试试这个:

I have a form, where different validations can apply, depending on the parameter action, stored in VUEX store. I try this:

data: function() {
   const validations = {
      sendToProject: {
        cardProject: {
          required,
        },
      },
      recallToBranch: {
        fioReceiver: {
          required,
        }
      }
   }
   return {
     validations,
   }
},
validations() {
  return {
    q: this.validations[this.action]  // supposed to be this.validations['sendToProject']
  }
},
computed: {
  ...mapGetters({
    action: 'action',
  }),
},

这确实有效,但在引导时抛出错误:

This actually works, but throws an error while bootstraping:

[Vue 警告]:渲染函数出错:TypeError: can't convert undefined to object"

该错误会阻止非 Vue 代码(Bootstrap jQuery 插件初始化等)执行.

and that error prevents non-Vue code (Bootstrap jQuery plugins initializations, etc) from execution.

如何解决?谢谢.

推荐答案

您是否尝试过在 required 验证器中使用 requiredIf.例如:

Have you tried to use requiredIf in required validator. For example:

validations: {
  anyProp: {
    required: requiredIf(function (abc) {
      return abc > 10 && abc < 20
    })
  }
}

这篇关于使用 Vuelidate 进行条件验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆