Skip to content

Commit d50a756

Browse files
committed
support async (promised) validators #170
1 parent 19d5e5a commit d50a756

File tree

4 files changed

+37
-11
lines changed

4 files changed

+37
-11
lines changed

dev/full/schema.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@ import {
44
validators
55
} from "../../src";
66

7+
let customAsyncValidator = function(value) {
8+
return new Promise((resolve, reject) => {
9+
setTimeout(() => {
10+
if (value)
11+
resolve();
12+
else
13+
resolve([ "Invalid value from async validator" ]);
14+
}, 1000);
15+
});
16+
};
17+
718
module.exports = {
819
fields: [
920

@@ -56,7 +67,7 @@ module.exports = {
5667
model: "website",
5768
placeholder: "Enter your website",
5869
inputName: "website",
59-
validator: validators.url
70+
validator: customAsyncValidator //validators.url
6071
}, {
6172
type: "input",
6273
inputType: "tel",
@@ -120,8 +131,8 @@ module.exports = {
120131
inputType: "number",
121132
label: "Number",
122133
model: "age",
123-
styleClasses: "half-width",
124-
validator: validators.number
134+
styleClasses: "half-width"
135+
//validator: validators.number
125136
}, {
126137
type: "input",
127138
inputType: "range",
@@ -312,7 +323,8 @@ module.exports = {
312323
rows: 4,
313324
validator: validators.string
314325
}, {
315-
type: "text",
326+
type: "input",
327+
inputType: "text",
316328
label: "Field with buttons",
317329
model: "address.geo",
318330
disabled: false,

dist/vfg-core.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/vfg.js

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/fields/abstractField.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,26 @@ export default {
9090
}
9191

9292
each(validators, (validator) => {
93-
let err = validator(this.value, this.schema, this.model);
94-
if (err) {
93+
let addErrors = err => {
9594
if (isArray(err))
9695
Array.prototype.push.apply(this.errors, err);
9796
else if (isString(err))
9897
this.errors.push(err);
98+
};
99+
100+
let res = validator(this.value, this.schema, this.model);
101+
if (res && isFunction(res.then)) {
102+
// It is a Promise, async validator
103+
res.then(err => {
104+
if (err) {
105+
addErrors(err);
106+
let isValid = this.errors.length == 0;
107+
this.$emit("validated", isValid, this.errors, this);
108+
}
109+
});
110+
} else {
111+
if (res)
112+
addErrors(res);
99113
}
100114
});
101115

0 commit comments

Comments
 (0)