Vue.js v-validate从模型中获取价值

时间:2017-03-21 15:32:13

标签: javascript vue.js vuejs2

如何将值从模型传递到v-validate(VeeValidate)指令。

 v-validate="'required|unique-email:model.Email'"

显然,我想将model.Email替换为data的变量。

此处验证代码

VeeValidate.Validator.extend('unique-email', {
    messages: {
        en: (field, args) => {
            return "Email Taken";
        }
    },
    validate: (value, args) => {
        return profileService.emailTaken(value).then((response) => {
            console.log(args); <-- i need email model in here,
            var emailTaken = response.data;
            return new Promise(resolve => {
                resolve({
                    valid: !emailTaken
                    //data: value !== 'trigger' ? undefined : { message: 'Not this value' }
                });
            });
        });
    }
});

1 个答案:

答案 0 :(得分:1)

最简单的方法是

v-validate="'required|unique-email:' + model.Email"

这会将model.Email添加为

args数组的第一个元素
validate: (value, args)

另外,它的价值,

        return new Promise(resolve => {
            resolve({
                valid: !emailTaken
                //data: value !== 'trigger' ? undefined : { message: 'Not this value' }
            });
        });

可以缩短为

return Promise.resolve({valid: !emailTaken});