Reputation: 3334
I have in a form multiple inputs that are validated by vee-validate, it works well, but I want to only check some fields on an event. So after seeing one issue about this https://github.com/baianat/vee-validate/issues/1089, I wrote this function:
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);
}
The idea is to get an array with the error messages of the fields that failed. Note that I used some console.log to be sure that when the check of the input fails, it returns the error message. But when I use it:
this.checkInputs().then(function(results) {
console.log(results);
});
I only have an array with 4 undefined values instead of an array of 4 strings for the errors messages.
Upvotes: 0
Views: 1755
Reputation: 1
Array.map()
should return value, also this.$validator.validate(input)
return promise, so you could wrote something like this:
inputs.map(async input => {
if(await this.$validator.validate(input) === true)
return null;
else
return dictionary.custom[input].required();
})
Upvotes: 0
Reputation: 337
Have you tried $nextTick
?
this.$nextTick(function() {
this.checkInputs().then(function(results) {
console.log(results);
});
});
Upvotes: 2