Commit 1231c1ab645d6698e11787b81eb58083a9f7767e

Authored by Aresn
Committed by GitHub
2 parents 5a83c4ae 0c0683e4

Merge pull request #2018 from Baoyx007/patch-1

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,18 +57,24 @@
57 }); 57 });
58 }, 58 },
59 validate(callback) { 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 validateField(prop, cb) { 79 validateField(prop, cb) {
74 const field = this.fields.filter(field => field.prop === prop)[0]; 80 const field = this.fields.filter(field => field.prop === prop)[0];