最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Conditional validation with Vuelidate? - Stack Overflow

programmeradmin0浏览0评论

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 warn]: Error in render function: "TypeError: can't convert undefined to object"

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

How to fix?

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 warn]: Error in render function: "TypeError: can't convert undefined to object"

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

How to fix?

Share Improve this question edited Aug 30, 2024 at 14:27 Luke Girvin 13.4k10 gold badges67 silver badges85 bronze badges asked Dec 4, 2017 at 10:09 SevaSeva 6852 gold badges7 silver badges18 bronze badges 1
  • 1 I hate to say this, but Vuelidate is really a bad library. I had the same issues so I changed to github.com/baianat/vee-validate and it's much better – samayo Commented Dec 4, 2017 at 10:13
Add a comment  | 

1 Answer 1

Reset to default 19

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

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

评论列表(0)

  1. 暂无评论