Skip to content

Commit 08bd3a4

Browse files
committed
feat(forms): add form builder
1 parent 10fb7bb commit 08bd3a4

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection';
2+
import {isPresent} from 'angular2/src/facade/lang';
3+
import {ControlGroup, Control, OptionalControl, OptionalControlGroup} from 'angular2/forms';
4+
5+
6+
export class FormBuilder {
7+
group(controlsConfig, extra = null):ControlGroup {
8+
var controls = this._reduceControls(controlsConfig);
9+
var optionals = isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null;
10+
var validator = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null;
11+
12+
if (isPresent(validator)) {
13+
return new ControlGroup(controls, optionals, validator);
14+
} else {
15+
return new ControlGroup(controls, optionals);
16+
}
17+
}
18+
19+
control(value, validator:Function = null):Control {
20+
if (isPresent(validator)) {
21+
return new Control(value, validator);
22+
} else {
23+
return new Control(value);
24+
}
25+
}
26+
27+
_reduceControls(controlsConfig) {
28+
var controls = {};
29+
StringMapWrapper.forEach(controlsConfig, (controlConfig, controlName) => {
30+
controls[controlName] = this._createControl(controlConfig);
31+
});
32+
return controls;
33+
}
34+
35+
_createControl(controlConfig) {
36+
if (controlConfig instanceof Control || controlConfig instanceof ControlGroup) {
37+
return controlConfig;
38+
39+
} else if (ListWrapper.isList(controlConfig)) {
40+
var value = ListWrapper.get(controlConfig, 0);
41+
var validator = controlConfig.length > 1 ? controlConfig[1] : null;
42+
return this.control(value, validator);
43+
44+
} else {
45+
return this.control(controlConfig);
46+
}
47+
}
48+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib';
2+
import {Control, FormBuilder} from 'angular2/forms';
3+
import * as validations from 'angular2/forms';
4+
5+
export function main() {
6+
describe("Form Builder", () => {
7+
var b;
8+
9+
beforeEach(() => {
10+
b = new FormBuilder();
11+
});
12+
13+
it("should create controls from a value", () => {
14+
var g = b.group({
15+
"login": "some value"
16+
});
17+
18+
expect(g.controls["login"].value).toEqual("some value");
19+
});
20+
21+
it("should create controls from an array", () => {
22+
var g = b.group({
23+
"login": ["some value"],
24+
"password": ["some value", validations.required]
25+
});
26+
27+
expect(g.controls["login"].value).toEqual("some value");
28+
expect(g.controls["password"].value).toEqual("some value");
29+
expect(g.controls["password"].validator).toEqual(validations.required);
30+
});
31+
32+
it("should use controls", () => {
33+
var g = b.group({
34+
"login": b.control("some value", validations.required)
35+
});
36+
37+
expect(g.controls["login"].value).toEqual("some value");
38+
expect(g.controls["login"].validator).toBe(validations.required);
39+
});
40+
41+
it("should create groups with optional controls", () => {
42+
var g = b.group({
43+
"login": "some value"
44+
}, {"optionals": {"login" : false}});
45+
46+
expect(g.contains("login")).toEqual(false);
47+
});
48+
49+
it("should create groups with a custom validator", () => {
50+
var g = b.group({
51+
"login": "some value"
52+
}, {"validator": validations.nullValidator});
53+
54+
expect(g.validator).toBe(validations.nullValidator);
55+
});
56+
57+
it("should use default validators when no validators are provided", () => {
58+
var g = b.group({
59+
"login": "some value"
60+
});
61+
expect(g.controls["login"].validator).toBe(validations.nullValidator);
62+
expect(g.validator).toBe(validations.controlGroupValidator);
63+
});
64+
});
65+
}

0 commit comments

Comments
 (0)