[英]Vee-validate Validate only some fields
我在一個表單中有多個由 vee-validate 驗證的輸入,它運行良好,但我只想檢查事件的某些字段。 所以在看到一個關於這個https://github.com/baianat/vee-validate/issues/1089 的問題后,我寫了這個函數:
async checkInputs(){
let inputs = ['input1', 'input2', 'input3', 'input4'];
const results = Promise.all(
inputs.map(input => {
if(this.$validator.validate(input) === true)
null;
else
dictionary.custom[input].required();
})
);
return (await results).filter(element => element != null);
}
這個想法是獲取一個包含失敗字段的錯誤消息的數組。 請注意,我使用了一些 console.log 來確保當輸入檢查失敗時,它會返回錯誤消息。 但是當我使用它時:
this.checkInputs().then(function(results) {
console.log(results);
});
對於錯誤消息,我只有一個包含 4 個未定義值的數組,而不是包含 4 個字符串的數組。
您是否嘗試過$nextTick
?
this.$nextTick(function() {
this.checkInputs().then(function(results) {
console.log(results);
});
});
Array.map()
應該返回值, this.$validator.validate(input)
返回 promise,所以你可以寫這樣的東西:
inputs.map(async input => {
if(await this.$validator.validate(input) === true)
return null;
else
return dictionary.custom[input].required();
})
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.