From cf9cb616655580c8957c30f8c4cea0ba5fc20c7c Mon Sep 17 00:00:00 2001 From: vsavkin Date: Wed, 25 Feb 2015 12:54:27 -0800 Subject: [PATCH] clean(forms): cleanup --- modules/angular2/src/facade/collection.dart | 5 +---- modules/angular2/src/forms/model.js | 21 +++++++++--------- .../src/forms/validator_directives.js | 4 +--- modules/angular2/src/forms/validators.js | 7 +++++- modules/angular2/test/forms/model_spec.js | 7 +++--- .../angular2/test/forms/validators_spec.js | 22 +++++++++---------- 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/modules/angular2/src/facade/collection.dart b/modules/angular2/src/facade/collection.dart index 71cdccb2af..0e0ed6abb0 100644 --- a/modules/angular2/src/facade/collection.dart +++ b/modules/angular2/src/facade/collection.dart @@ -68,11 +68,8 @@ class StringMapWrapper { m.forEach((k, v) => fn(v, k)); } static HashMap merge(HashMap a, HashMap b) { - var m = {}; - - a.forEach((k, v) => m[k] = v); + var m = new HashMap.from(a); b.forEach((k, v) => m[k] = v); - return m; } static bool isEmpty(Map m) => m.isEmpty; diff --git a/modules/angular2/src/forms/model.js b/modules/angular2/src/forms/model.js index 982b21e659..11212e9be4 100644 --- a/modules/angular2/src/forms/model.js +++ b/modules/angular2/src/forms/model.js @@ -9,6 +9,7 @@ export const INVALID = "INVALID"; // get value():any; // validator:Function; // get status():string; +// get valid():boolean; // get errors():Map; // get active():boolean {} // updateValue(value:any){} @@ -19,19 +20,19 @@ export class Control { _value:any; _status:string; _errors; - _updated:boolean; + _dirty:boolean; _parent:ControlGroup; validator:Function; constructor(value:any, validator:Function = nullValidator) { this._value = value; this.validator = validator; - this._updated = true; + this._dirty = true; } updateValue(value:any) { this._value = value; - this._updated = true; + this._dirty = true; this._updateParent(); } @@ -63,8 +64,8 @@ export class Control { } _updateIfNeeded() { - if (this._updated) { - this._updated = false; + if (this._dirty) { + this._dirty = false; this._errors = this.validator(this); this._status = isPresent(this._errors) ? INVALID : VALID; } @@ -81,14 +82,14 @@ export class ControlGroup { _value:any; _status:string; _errors; - _updated:boolean; + _dirty:boolean; validator:Function; controls; constructor(controls, validator:Function = controlGroupValidator) { this.controls = controls; this.validator = validator; - this._updated = true; + this._dirty = true; this._setParentForControls(); } @@ -119,8 +120,8 @@ export class ControlGroup { } _updateIfNeeded() { - if (this._updated) { - this._updated = false; + if (this._dirty) { + this._dirty = false; this._value = this._reduceValue(); this._errors = this.validator(this); this._status = isPresent(this._errors) ? INVALID : VALID; @@ -138,7 +139,7 @@ export class ControlGroup { } _controlChanged() { - this._updated = true; + this._dirty = true; } } diff --git a/modules/angular2/src/forms/validator_directives.js b/modules/angular2/src/forms/validator_directives.js index 28a7d0d1fa..de8b58103f 100644 --- a/modules/angular2/src/forms/validator_directives.js +++ b/modules/angular2/src/forms/validator_directives.js @@ -1,8 +1,6 @@ -import {isBlank, isPresent} from 'angular2/src/facade/lang'; -import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection'; import {Decorator} from 'angular2/core'; -import {ControlGroup, Control, ControlDirective} from 'angular2/forms'; +import {ControlDirective} from 'angular2/forms'; import * as validators from 'angular2/forms'; @Decorator({ diff --git a/modules/angular2/src/forms/validators.js b/modules/angular2/src/forms/validators.js index f49a75a58d..1df7d22b93 100644 --- a/modules/angular2/src/forms/validators.js +++ b/modules/angular2/src/forms/validators.js @@ -25,7 +25,12 @@ export function controlGroupValidator(c:ControlGroup) { var res = {}; StringMapWrapper.forEach(c.controls, (control, name) => { if (control.active && isPresent(control.errors)) { - res[name] = control.errors; + StringMapWrapper.forEach(control.errors, (value, error) => { + if (! StringMapWrapper.contains(res, error)) { + res[error] = []; + } + ListWrapper.push(res[error], control); + }); } }); return StringMapWrapper.isEmpty(res) ? null : res; diff --git a/modules/angular2/test/forms/model_spec.js b/modules/angular2/test/forms/model_spec.js index 8ef2f7bc21..fa24809a84 100644 --- a/modules/angular2/test/forms/model_spec.js +++ b/modules/angular2/test/forms/model_spec.js @@ -51,13 +51,12 @@ export function main() { }); it("should run the validator with the initial value (invalid)", () => { - var g = new ControlGroup({ - "one": new Control(null, validations.required) - }); + var one = new Control(null, validations.required); + var g = new ControlGroup({"one": one}); expect(g.valid).toEqual(false); - expect(g.errors).toEqual({"one": {"required" : true}}); + expect(g.errors).toEqual({"required": [one]}); }); it("should run the validator with the value changes", () => { diff --git a/modules/angular2/test/forms/validators_spec.js b/modules/angular2/test/forms/validators_spec.js index be3ccbb865..5300ff4dce 100644 --- a/modules/angular2/test/forms/validators_spec.js +++ b/modules/angular2/test/forms/validators_spec.js @@ -44,25 +44,23 @@ export function main() { describe("controlGroupValidator", () => { it("should collect errors from the child controls", () => { - var g = new ControlGroup({ - "one" : new Control("one", validator("a", true)), - "two" : new Control("two", validator("b", true)) - }); + var one = new Control("one", validator("a", true)); + var two = new Control("one", validator("b", true)); + var g = new ControlGroup({"one" : one, "two" : two}); expect(controlGroupValidator(g)).toEqual({ - "one" : {"a" : true}, - "two" : {"b" : true} + "a" : [one], + "b" : [two] }); }); - it("should not include keys for controls that have no errors", () => { - var g = new ControlGroup({ - "one" : new Control("one", validator("a", true)), - "two" : new Control("one") - }); + it("should not include controls that have no errors", () => { + var one = new Control("one", validator("a", true)); + var two = new Control("two"); + var g = new ControlGroup({"one" : one, "two" : two}); expect(controlGroupValidator(g)).toEqual({ - "one" : {"a" : true} + "a": [one] }); });