X Tutup
Skip to content

Commit 1c322f1

Browse files
committed
feat(forms): update FormBuilder to support async validations
Closes #5020
1 parent 31c12af commit 1c322f1

File tree

2 files changed

+33
-31
lines changed

2 files changed

+33
-31
lines changed

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

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import * as modelModule from './model';
3434
* login: ["", Validators.required],
3535
* passwordRetry: builder.group({
3636
* password: ["", Validators.required],
37-
* passwordConfirmation: ["", Validators.required]
37+
* passwordConfirmation: ["", Validators.required, asyncValidator]
3838
* })
3939
* });
4040
* }
@@ -58,36 +58,25 @@ export class FormBuilder {
5858
var controls = this._reduceControls(controlsConfig);
5959
var optionals = isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null;
6060
var validator = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null;
61-
62-
if (isPresent(validator)) {
63-
return new modelModule.ControlGroup(controls, optionals, validator);
64-
} else {
65-
return new modelModule.ControlGroup(controls, optionals);
66-
}
61+
var asyncValidator = isPresent(extra) ? StringMapWrapper.get(extra, "asyncValidator") : null;
62+
return new modelModule.ControlGroup(controls, optionals, validator, asyncValidator);
6763
}
68-
6964
/**
70-
* Construct a new {@link Control} with the given `value` and `validator`.
65+
* Construct a new {@link Control} with the given `value`,`validator`, and `asyncValidator`.
7166
*/
72-
control(value: Object, validator: Function = null): modelModule.Control {
73-
if (isPresent(validator)) {
74-
return new modelModule.Control(value, validator);
75-
} else {
76-
return new modelModule.Control(value);
77-
}
67+
control(value: Object, validator: Function = null,
68+
asyncValidator: Function = null): modelModule.Control {
69+
return new modelModule.Control(value, validator, asyncValidator);
7870
}
7971

8072
/**
8173
* Construct an array of {@link Control}s from the given `controlsConfig` array of
82-
* configuration, with the given optional `validator`.
74+
* configuration, with the given optional `validator` and `asyncValidator`.
8375
*/
84-
array(controlsConfig: any[], validator: Function = null): modelModule.ControlArray {
76+
array(controlsConfig: any[], validator: Function = null,
77+
asyncValidator: Function = null): modelModule.ControlArray {
8578
var controls = controlsConfig.map(c => this._createControl(c));
86-
if (isPresent(validator)) {
87-
return new modelModule.ControlArray(controls, validator);
88-
} else {
89-
return new modelModule.ControlArray(controls);
90-
}
79+
return new modelModule.ControlArray(controls, validator, asyncValidator);
9180
}
9281

9382
/** @internal */
@@ -109,7 +98,8 @@ export class FormBuilder {
10998
} else if (isArray(controlConfig)) {
11099
var value = controlConfig[0];
111100
var validator = controlConfig.length > 1 ? controlConfig[1] : null;
112-
return this.control(value, validator);
101+
var asyncValidator = controlConfig.length > 2 ? controlConfig[2] : null;
102+
return this.control(value, validator, asyncValidator);
113103

114104
} else {
115105
return this.control(controlConfig);

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,13 @@ import {
99
afterEach,
1010
el
1111
} from 'angular2/testing_internal';
12-
import {Control, FormBuilder, Validators} from 'angular2/core';
12+
import {Control, FormBuilder} from 'angular2/core';
13+
import {PromiseWrapper} from 'angular2/src/core/facade/promise';
1314

1415
export function main() {
16+
function syncValidator(_) { return null; }
17+
function asyncValidator(_) { return PromiseWrapper.resolve(null); }
18+
1519
describe("Form Builder", () => {
1620
var b;
1721

@@ -24,18 +28,21 @@ export function main() {
2428
});
2529

2630
it("should create controls from an array", () => {
27-
var g = b.group({"login": ["some value"], "password": ["some value", Validators.required]});
31+
var g = b.group(
32+
{"login": ["some value"], "password": ["some value", syncValidator, asyncValidator]});
2833

2934
expect(g.controls["login"].value).toEqual("some value");
3035
expect(g.controls["password"].value).toEqual("some value");
31-
expect(g.controls["password"].validator).toEqual(Validators.required);
36+
expect(g.controls["password"].validator).toEqual(syncValidator);
37+
expect(g.controls["password"].asyncValidator).toEqual(asyncValidator);
3238
});
3339

3440
it("should use controls", () => {
35-
var g = b.group({"login": b.control("some value", Validators.required)});
41+
var g = b.group({"login": b.control("some value", syncValidator, asyncValidator)});
3642

3743
expect(g.controls["login"].value).toEqual("some value");
38-
expect(g.controls["login"].validator).toBe(Validators.required);
44+
expect(g.controls["login"].validator).toBe(syncValidator);
45+
expect(g.controls["login"].asyncValidator).toBe(asyncValidator);
3946
});
4047

4148
it("should create groups with optional controls", () => {
@@ -45,16 +52,21 @@ export function main() {
4552
});
4653

4754
it("should create groups with a custom validator", () => {
48-
var g = b.group({"login": "some value"}, {"validator": Validators.nullValidator});
55+
var g = b.group({"login": "some value"},
56+
{"validator": syncValidator, "asyncValidator": asyncValidator});
4957

50-
expect(g.validator).toBe(Validators.nullValidator);
58+
expect(g.validator).toBe(syncValidator);
59+
expect(g.asyncValidator).toBe(asyncValidator);
5160
});
5261

5362
it("should create control arrays", () => {
5463
var c = b.control("three");
55-
var a = b.array(["one", ["two", Validators.required], c, b.array(['four'])]);
64+
var a = b.array(["one", ["two", syncValidator], c, b.array(['four'])], syncValidator,
65+
asyncValidator);
5666

5767
expect(a.value).toEqual(['one', 'two', 'three', ['four']]);
68+
expect(a.validator).toBe(syncValidator);
69+
expect(a.asyncValidator).toBe(asyncValidator);
5870
});
5971
});
6072
}

0 commit comments

Comments
 (0)
X Tutup