Commit 0c0683e4e3fba64f1160737e89bcf2c704fd0dc7
Committed by
GitHub
1 parent
fa5ea60b
feat(form) : support promise for validate method #1857
Showing
1 changed file
with
17 additions
and
11 deletions
Show diff stats
src/components/form/form.vue
... | ... | @@ -57,18 +57,24 @@ |
57 | 57 | }); |
58 | 58 | }, |
59 | 59 | validate(callback) { |
60 | - let valid = true; | |
61 | - let count = 0; | |
62 | - this.fields.forEach(field => { | |
63 | - field.validate('', errors => { | |
64 | - if (errors) { | |
65 | - valid = false; | |
66 | - } | |
67 | - if (typeof callback === 'function' && ++count === this.fields.length) { | |
68 | - callback(valid); | |
69 | - } | |
60 | + return new Promise(resolve => { | |
61 | + let valid = true; | |
62 | + let count = 0; | |
63 | + this.fields.forEach(field => { | |
64 | + field.validate('', errors => { | |
65 | + if (errors) { | |
66 | + valid = false; | |
67 | + } | |
68 | + if (++count === this.fields.length) { | |
69 | + // all finish | |
70 | + resolve(valid) | |
71 | + if (typeof callback === 'function') { | |
72 | + callback(valid); | |
73 | + } | |
74 | + } | |
75 | + }); | |
70 | 76 | }); |
71 | - }); | |
77 | + }) | |
72 | 78 | }, |
73 | 79 | validateField(prop, cb) { |
74 | 80 | const field = this.fields.filter(field => field.prop === prop)[0]; | ... | ... |