Search code examples
javascriptvuejs2vuexvee-validate

Vee validate get previous value after vue set


I want to update v-model value by code and validate that field. So I am using vue.set method for updating value and then call $validator.validate. my code is something like that.

Vue.set(model,property, value);
vm.$validator.validate(property).then(function (valid) {
            if (!valid) {
                vm.$validator.flag(property, {
                    touched: true,
                    dirty: true
                });
            } 
        });

my validation rules code is somethng like that:

Validator.extend("val_compulsory", {
getMessage(field, args) {
    return args[0];
},
validate(value) {
    return {
        valid: !!value,
        data: {
            required: true
        }
    };
}
}, { computesRequired: true });

but in val_compulsory validator I always get previous value which is before vue.set. Is there any way to get latest value in vee-validator validation methods after vue.set?


Solution

  • Try this:

    Vue.set(model,property, value);
    vm.$nextTick(function() {
        vm.$validator.validate(property).then(function (valid) {
            if (!valid) {
                vm.$validator.flag(property, {
                    touched: true,
                    dirty: true
                });
            } 
        });
    });