Commit 476d1c4f authored by Allenice's avatar Allenice Committed by 杨奕

Form: fix async validate bug

parent 0c47e6d3
...@@ -80,7 +80,19 @@ ...@@ -80,7 +80,19 @@
console.warn('[Element Warn][Form]model is required for validate to work!'); console.warn('[Element Warn][Form]model is required for validate to work!');
return; return;
} }
let promise;
// if no callback, return promise
if (typeof callback !== 'function' && window.Promise) {
promise = new window.Promise((resolve, reject) => {
callback = function(valid) {
resolve(valid);
};
});
}
let valid = true; let valid = true;
let count = 0;
// 如果需要验证的fields为空,调用验证时立刻返回callback // 如果需要验证的fields为空,调用验证时立刻返回callback
if (this.fields.length === 0 && callback) { if (this.fields.length === 0 && callback) {
callback(true); callback(true);
...@@ -90,13 +102,14 @@ ...@@ -90,13 +102,14 @@
if (errors) { if (errors) {
valid = false; valid = false;
} }
if (typeof callback === 'function' && ++count === this.fields.length) {
callback(valid);
}
}); });
}); });
if (typeof callback === 'function') { if (promise) {
callback(valid); return promise;
} else if (window.Promise) {
return Promise[valid ? 'resolve' : 'reject'](valid); // eslint-disable-line
} }
}, },
validateField(prop, cb) { validateField(prop, cb) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment