X Tutup
Skip to content

Commit cf449dd

Browse files
committed
feat(forms): implements a combinator for composing async validators
1 parent 53bd6e1 commit cf449dd

File tree

2 files changed

+84
-8
lines changed

2 files changed

+84
-8
lines changed

modules/angular2/src/core/forms/validators.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import {ListWrapper, StringMapWrapper} from 'angular2/src/core/facade/collection
33
import {OpaqueToken} from 'angular2/src/core/di';
44

55
import * as modelModule from './model';
6+
import {PromiseWrapper} from "../facade/promise";
67

78
/**
89
* Providers for validators to be used for {@link Control}s in a form.
@@ -18,6 +19,7 @@ import * as modelModule from './model';
1819
* ```
1920
*/
2021
export const NG_VALIDATORS: OpaqueToken = CONST_EXPR(new OpaqueToken("NgValidators"));
22+
export const NG_ASYNC_VALIDATORS: OpaqueToken = CONST_EXPR(new OpaqueToken("NgAsyncValidators"));
2123

2224
/**
2325
* Provides a set of validators used by form controls.
@@ -76,14 +78,33 @@ export class Validators {
7678
* of the individual error maps.
7779
*/
7880
static compose(validators: Function[]): Function {
79-
if (isBlank(validators)) return Validators.nullValidator;
81+
if (isBlank(validators)) return null;
82+
var presentValidators = ListWrapper.filter(validators, isPresent);
83+
if (presentValidators.length == 0) return null;
8084

8185
return function(control: modelModule.AbstractControl) {
82-
var res = ListWrapper.reduce(validators, (res, validator) => {
83-
var errors = isPresent(validator) ? validator(control) : null;
84-
return isPresent(errors) ? StringMapWrapper.merge(<any>res, <any>errors) : res;
85-
}, {});
86-
return StringMapWrapper.isEmpty(res) ? null : res;
86+
return _mergeErrors(_executeValidators(control, presentValidators));
8787
};
8888
}
89+
90+
static composeAsync(validators: Function[]): Function {
91+
if (isBlank(validators)) return null;
92+
var presentValidators = ListWrapper.filter(validators, isPresent);
93+
if (presentValidators.length == 0) return null;
94+
95+
return function(control: modelModule.AbstractControl) {
96+
return PromiseWrapper.all(_executeValidators(control, presentValidators)).then(_mergeErrors);
97+
};
98+
}
99+
}
100+
101+
function _executeValidators(control: modelModule.AbstractControl, validators: Function[]): any[] {
102+
return validators.map(v => v(control));
103+
}
104+
105+
function _mergeErrors(arrayOfErrors: any[]): {[key: string]: any} {
106+
var res = ListWrapper.reduce(arrayOfErrors, (res, errors) => {
107+
return isPresent(errors) ? StringMapWrapper.merge(<any>res, <any>errors) : res;
108+
}, {});
109+
return StringMapWrapper.isEmpty(res) ? null : res;
89110
}

modules/angular2/test/core/forms/validators_spec.ts

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,14 @@ import {
77
expect,
88
beforeEach,
99
afterEach,
10+
fakeAsync,
11+
tick,
1012
el
1113
} from 'angular2/testing_internal';
1214
import {ControlGroup, Control, Validators, AbstractControl, ControlArray} from 'angular2/core';
15+
import {PromiseWrapper} from 'angular2/src/core/facade/promise';
16+
import {TimerWrapper} from 'angular2/src/core/facade/async';
17+
import {CONST_EXPR} from 'angular2/src/core/facade/lang';
1318

1419
export function main() {
1520
function validator(key: string, error: any) {
@@ -65,8 +70,8 @@ export function main() {
6570
});
6671

6772
describe("compose", () => {
68-
it("should return a null validator when given null",
69-
() => { expect(Validators.compose(null)).toBe(Validators.nullValidator); });
73+
it("should return null when given null",
74+
() => { expect(Validators.compose(null)).toBe(null); });
7075

7176
it("should collect errors from all the validators", () => {
7277
var c = Validators.compose([validator("a", true), validator("b", true)]);
@@ -88,5 +93,55 @@ export function main() {
8893
expect(c(new Control(""))).toEqual({"required": true});
8994
});
9095
});
96+
97+
describe("composeAsync", () => {
98+
function asyncValidator(expected, response, timeout = 0) {
99+
return (c) => {
100+
var completer = PromiseWrapper.completer();
101+
var res = c.value != expected ? response : null;
102+
TimerWrapper.setTimeout(() => { completer.resolve(res); }, timeout);
103+
return completer.promise;
104+
};
105+
}
106+
107+
it("should return null when given null",
108+
() => { expect(Validators.composeAsync(null)).toEqual(null); });
109+
110+
it("should collect errors from all the validators", fakeAsync(() => {
111+
var c = Validators.composeAsync([
112+
asyncValidator("expected", {"one": true}),
113+
asyncValidator("expected", {"two": true})
114+
]);
115+
116+
var value = null;
117+
c(new Control("invalid")).then(v => value = v);
118+
119+
tick(1);
120+
121+
expect(value).toEqual({"one": true, "two": true});
122+
}));
123+
124+
it("should return null when no errors", fakeAsync(() => {
125+
var c = Validators.composeAsync([asyncValidator("expected", {"one": true})]);
126+
127+
var value = null;
128+
c(new Control("expected")).then(v => value = v);
129+
130+
tick(1);
131+
132+
expect(value).toEqual(null);
133+
}));
134+
135+
it("should ignore nulls", fakeAsync(() => {
136+
var c = Validators.composeAsync([asyncValidator("expected", {"one": true}), null]);
137+
138+
var value = null;
139+
c(new Control("invalid")).then(v => value = v);
140+
141+
tick(1);
142+
143+
expect(value).toEqual({"one": true});
144+
}));
145+
});
91146
});
92147
}

0 commit comments

Comments
 (0)
X Tutup