Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional validation with Vuelidate?

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?

like image 907
Seva Avatar asked Aug 26 '26 01:08

Seva


1 Answers

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

validations: {
  anyProp: {
    required: requiredIf(function (abc) {
      return abc > 10 && abc < 20
    })
  }
}
like image 79
Bernard Doci Avatar answered Aug 28 '26 15:08

Bernard Doci



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!