Search code examples
vue.jsvuelidate

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)
    },
  },
}

Solution

  • 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)
          },
        }
      }
    }