如何将值从模型传递到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' }
});
});
});
}
});
答案 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});