From 0c0683e4e3fba64f1160737e89bcf2c704fd0dc7 Mon Sep 17 00:00:00 2001 From: Haven Date: Thu, 28 Sep 2017 00:40:16 -0500 Subject: [PATCH] feat(form) : support promise for validate method #1857 --- src/components/form/form.vue | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/src/components/form/form.vue b/src/components/form/form.vue index 1103075..f9b4fa6 100644 --- a/src/components/form/form.vue +++ b/src/components/form/form.vue @@ -57,18 +57,24 @@ }); }, validate(callback) { - let valid = true; - let count = 0; - this.fields.forEach(field => { - field.validate('', errors => { - if (errors) { - valid = false; - } - if (typeof callback === 'function' && ++count === this.fields.length) { - callback(valid); - } + return new Promise(resolve => { + let valid = true; + let count = 0; + this.fields.forEach(field => { + field.validate('', errors => { + if (errors) { + valid = false; + } + if (++count === this.fields.length) { + // all finish + resolve(valid) + if (typeof callback === 'function') { + callback(valid); + } + } + }); }); - }); + }) }, validateField(prop, cb) { const field = this.fields.filter(field => field.prop === prop)[0]; -- libgit2 0.21.4