Beusebiu
Beusebiu

Reputation: 1513

Use Vuejs variable in Validations

I try to do a simple maxValue, but I am not sure how can I use a value from vue data. URL for package.

import { required, maxValue } from "vuelidate/lib/validators";

export default {
  data() {
    return {
      totalClauses: 10,
    };
  },
  validations: {
    nr_of_clauses: {
      required,
      maxValue: maxValue(this.totalClauses)
    },
  },
}

Upvotes: 1

Views: 820

Answers (2)

Luis Pereira
Luis Pereira

Reputation: 9

I know that the answer for this issue was found, but i want to quickly recommend changing from Vuelidate to VeeValidate. I've used both and Vuelidate has some limitations that VeeValidate solves like custom validations(with messages for each validation) and it's more simple to use.

You can find Veevalidate documentation here

Upvotes: 1

Yom T.
Yom T.

Reputation: 9180

Validations schema can be a function, which will make it dynamic and possibly dependant on your model's data. [source]

So,

import { required, maxValue } from "vuelidate/lib/validators";

export default {
  data() {
    return {
      totalClauses: 10,
    };
  },

  validations() {
    return {
      nr_of_clauses: {
        required,
        maxValue: maxValue(this.totalClauses)
      },
    }
  }
}

Upvotes: 1

Related Questions