admin管理员组文章数量:1314411
I have a scenario where I have sectioned out (scoped) a form so that I can validate small chunks at a time using the following function.
validateScope (scope) {
return this.$validator.validateAll(scope);
}
I want to do one final validation of the entire form before I submit it to the server; however, validateAll() doesn't seem to pick up inputs that have been added to a scope. I've also tried just validating each scope and then submit the form if they are ALL valid, but I am not sure how to do that since everything is asynchronous.
validateAll () {
let valid = true;
// Not sure how to build this function since validateScope is asynchronous
_.each(this.names, (name, index) => {
if (this.validateScope(`name-${index}`)) {
valid = false;
}
});
return valid; // Always returns true even though the _.each should set it to false
}
I have a scenario where I have sectioned out (scoped) a form so that I can validate small chunks at a time using the following function.
validateScope (scope) {
return this.$validator.validateAll(scope);
}
I want to do one final validation of the entire form before I submit it to the server; however, validateAll() doesn't seem to pick up inputs that have been added to a scope. I've also tried just validating each scope and then submit the form if they are ALL valid, but I am not sure how to do that since everything is asynchronous.
validateAll () {
let valid = true;
// Not sure how to build this function since validateScope is asynchronous
_.each(this.names, (name, index) => {
if (this.validateScope(`name-${index}`)) {
valid = false;
}
});
return valid; // Always returns true even though the _.each should set it to false
}
Share
Improve this question
edited Nov 16, 2022 at 9:51
Matthias Hryniszak
3,1624 gold badges39 silver badges51 bronze badges
asked Feb 28, 2018 at 15:06
Neve12ende12Neve12ende12
1,1944 gold badges18 silver badges35 bronze badges
5
- "pick up inputs that have been added to a scope." how are you adding inputs to a scope? – acdcjunior Commented Feb 28, 2018 at 15:18
-
You're going to need to collect all the promises returned from
validateScope
using something likePromise.all
and then evaluate whether one of them returned false or not. If you want to set up a working example, it would be easier to generate an answer. – Bert Commented Feb 28, 2018 at 15:19 - Where in the code is the asynchronous part? – Bergi Commented Feb 28, 2018 at 15:37
-
@bergi
$validator.validateAll
is a VeeValidate library function that returns a promise. – Bert Commented Feb 28, 2018 at 15:46 - @Bert Ah, I thought it was the function posted in the second snippet… – Bergi Commented Feb 28, 2018 at 16:00
1 Answer
Reset to default 5As mentioned in my ment, your code will end up looking something like this:
validateAll () {
let valid = true;
let validations = []
_.each(this.names, (name, index) => {
validations.push(this.validateScope('name-' + index))
});
return Promise.all(validations)
// consolidate the results into one Boolean
.then(results => results.every(r => r))
}
Then, of course, you'll have to use validateAll
as a promise:
this.validateAll().then(isValid => {
if (!isValid) {
//do whatever you need to do when something failed validation
} else {
// do whatever you need to do when everything is valid here
}
})
本文标签: javascriptVeeValidate validateAll() with scopeStack Overflow
版权声明:本文标题:javascript - Vee-Validate validateAll() with scope - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741902027a2403922.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论