From 08bd3a4443e146a7efaeaa26d7259b926e91ad4c Mon Sep 17 00:00:00 2001 From: vsavkin Date: Tue, 10 Mar 2015 18:12:50 -0700 Subject: [PATCH] feat(forms): add form builder --- modules/angular2/src/forms/form_builder.js | 48 ++++++++++++++ .../angular2/test/forms/form_builder_spec.js | 65 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 modules/angular2/src/forms/form_builder.js create mode 100644 modules/angular2/test/forms/form_builder_spec.js diff --git a/modules/angular2/src/forms/form_builder.js b/modules/angular2/src/forms/form_builder.js new file mode 100644 index 0000000000..c6e796e565 --- /dev/null +++ b/modules/angular2/src/forms/form_builder.js @@ -0,0 +1,48 @@ +import {StringMapWrapper, ListWrapper} from 'angular2/src/facade/collection'; +import {isPresent} from 'angular2/src/facade/lang'; +import {ControlGroup, Control, OptionalControl, OptionalControlGroup} from 'angular2/forms'; + + +export class FormBuilder { + group(controlsConfig, extra = null):ControlGroup { + var controls = this._reduceControls(controlsConfig); + var optionals = isPresent(extra) ? StringMapWrapper.get(extra, "optionals") : null; + var validator = isPresent(extra) ? StringMapWrapper.get(extra, "validator") : null; + + if (isPresent(validator)) { + return new ControlGroup(controls, optionals, validator); + } else { + return new ControlGroup(controls, optionals); + } + } + + control(value, validator:Function = null):Control { + if (isPresent(validator)) { + return new Control(value, validator); + } else { + return new Control(value); + } + } + + _reduceControls(controlsConfig) { + var controls = {}; + StringMapWrapper.forEach(controlsConfig, (controlConfig, controlName) => { + controls[controlName] = this._createControl(controlConfig); + }); + return controls; + } + + _createControl(controlConfig) { + if (controlConfig instanceof Control || controlConfig instanceof ControlGroup) { + return controlConfig; + + } else if (ListWrapper.isList(controlConfig)) { + var value = ListWrapper.get(controlConfig, 0); + var validator = controlConfig.length > 1 ? controlConfig[1] : null; + return this.control(value, validator); + + } else { + return this.control(controlConfig); + } + } +} \ No newline at end of file diff --git a/modules/angular2/test/forms/form_builder_spec.js b/modules/angular2/test/forms/form_builder_spec.js new file mode 100644 index 0000000000..94fda5f4b5 --- /dev/null +++ b/modules/angular2/test/forms/form_builder_spec.js @@ -0,0 +1,65 @@ +import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el} from 'angular2/test_lib'; +import {Control, FormBuilder} from 'angular2/forms'; +import * as validations from 'angular2/forms'; + +export function main() { + describe("Form Builder", () => { + var b; + + beforeEach(() => { + b = new FormBuilder(); + }); + + it("should create controls from a value", () => { + var g = b.group({ + "login": "some value" + }); + + expect(g.controls["login"].value).toEqual("some value"); + }); + + it("should create controls from an array", () => { + var g = b.group({ + "login": ["some value"], + "password": ["some value", validations.required] + }); + + expect(g.controls["login"].value).toEqual("some value"); + expect(g.controls["password"].value).toEqual("some value"); + expect(g.controls["password"].validator).toEqual(validations.required); + }); + + it("should use controls", () => { + var g = b.group({ + "login": b.control("some value", validations.required) + }); + + expect(g.controls["login"].value).toEqual("some value"); + expect(g.controls["login"].validator).toBe(validations.required); + }); + + it("should create groups with optional controls", () => { + var g = b.group({ + "login": "some value" + }, {"optionals": {"login" : false}}); + + expect(g.contains("login")).toEqual(false); + }); + + it("should create groups with a custom validator", () => { + var g = b.group({ + "login": "some value" + }, {"validator": validations.nullValidator}); + + expect(g.validator).toBe(validations.nullValidator); + }); + + it("should use default validators when no validators are provided", () => { + var g = b.group({ + "login": "some value" + }); + expect(g.controls["login"].validator).toBe(validations.nullValidator); + expect(g.validator).toBe(validations.controlGroupValidator); + }); + }); +} \ No newline at end of file