Skip to content

Commit cf9cb61

Browse files
committed
clean(forms): cleanup
1 parent f27e538 commit cf9cb61

File tree

6 files changed

+32
-34
lines changed

6 files changed

+32
-34
lines changed

modules/angular2/src/facade/collection.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,11 +68,8 @@ class StringMapWrapper {
6868
m.forEach((k, v) => fn(v, k));
6969
}
7070
static HashMap merge(HashMap a, HashMap b) {
71-
var m = {};
72-
73-
a.forEach((k, v) => m[k] = v);
71+
var m = new HashMap.from(a);
7472
b.forEach((k, v) => m[k] = v);
75-
7673
return m;
7774
}
7875
static bool isEmpty(Map m) => m.isEmpty;

modules/angular2/src/forms/model.js

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ export const INVALID = "INVALID";
99
// get value():any;
1010
// validator:Function;
1111
// get status():string;
12+
// get valid():boolean;
1213
// get errors():Map;
1314
// get active():boolean {}
1415
// updateValue(value:any){}
@@ -19,19 +20,19 @@ export class Control {
1920
_value:any;
2021
_status:string;
2122
_errors;
22-
_updated:boolean;
23+
_dirty:boolean;
2324
_parent:ControlGroup;
2425
validator:Function;
2526

2627
constructor(value:any, validator:Function = nullValidator) {
2728
this._value = value;
2829
this.validator = validator;
29-
this._updated = true;
30+
this._dirty = true;
3031
}
3132

3233
updateValue(value:any) {
3334
this._value = value;
34-
this._updated = true;
35+
this._dirty = true;
3536
this._updateParent();
3637
}
3738

@@ -63,8 +64,8 @@ export class Control {
6364
}
6465

6566
_updateIfNeeded() {
66-
if (this._updated) {
67-
this._updated = false;
67+
if (this._dirty) {
68+
this._dirty = false;
6869
this._errors = this.validator(this);
6970
this._status = isPresent(this._errors) ? INVALID : VALID;
7071
}
@@ -81,14 +82,14 @@ export class ControlGroup {
8182
_value:any;
8283
_status:string;
8384
_errors;
84-
_updated:boolean;
85+
_dirty:boolean;
8586
validator:Function;
8687
controls;
8788

8889
constructor(controls, validator:Function = controlGroupValidator) {
8990
this.controls = controls;
9091
this.validator = validator;
91-
this._updated = true;
92+
this._dirty = true;
9293
this._setParentForControls();
9394
}
9495

@@ -119,8 +120,8 @@ export class ControlGroup {
119120
}
120121

121122
_updateIfNeeded() {
122-
if (this._updated) {
123-
this._updated = false;
123+
if (this._dirty) {
124+
this._dirty = false;
124125
this._value = this._reduceValue();
125126
this._errors = this.validator(this);
126127
this._status = isPresent(this._errors) ? INVALID : VALID;
@@ -138,7 +139,7 @@ export class ControlGroup {
138139
}
139140

140141
_controlChanged() {
141-
this._updated = true;
142+
this._dirty = true;
142143
}
143144
}
144145

modules/angular2/src/forms/validator_directives.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import {isBlank, isPresent} from 'angular2/src/facade/lang';
2-
import {List, ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
31
import {Decorator} from 'angular2/core';
42

5-
import {ControlGroup, Control, ControlDirective} from 'angular2/forms';
3+
import {ControlDirective} from 'angular2/forms';
64
import * as validators from 'angular2/forms';
75

86
@Decorator({

modules/angular2/src/forms/validators.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ export function controlGroupValidator(c:ControlGroup) {
2525
var res = {};
2626
StringMapWrapper.forEach(c.controls, (control, name) => {
2727
if (control.active && isPresent(control.errors)) {
28-
res[name] = control.errors;
28+
StringMapWrapper.forEach(control.errors, (value, error) => {
29+
if (! StringMapWrapper.contains(res, error)) {
30+
res[error] = [];
31+
}
32+
ListWrapper.push(res[error], control);
33+
});
2934
}
3035
});
3136
return StringMapWrapper.isEmpty(res) ? null : res;

modules/angular2/test/forms/model_spec.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,12 @@ export function main() {
5151
});
5252

5353
it("should run the validator with the initial value (invalid)", () => {
54-
var g = new ControlGroup({
55-
"one": new Control(null, validations.required)
56-
});
54+
var one = new Control(null, validations.required);
55+
var g = new ControlGroup({"one": one});
5756

5857
expect(g.valid).toEqual(false);
5958

60-
expect(g.errors).toEqual({"one": {"required" : true}});
59+
expect(g.errors).toEqual({"required": [one]});
6160
});
6261

6362
it("should run the validator with the value changes", () => {

modules/angular2/test/forms/validators_spec.js

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,25 +44,23 @@ export function main() {
4444

4545
describe("controlGroupValidator", () => {
4646
it("should collect errors from the child controls", () => {
47-
var g = new ControlGroup({
48-
"one" : new Control("one", validator("a", true)),
49-
"two" : new Control("two", validator("b", true))
50-
});
47+
var one = new Control("one", validator("a", true));
48+
var two = new Control("one", validator("b", true));
49+
var g = new ControlGroup({"one" : one, "two" : two});
5150

5251
expect(controlGroupValidator(g)).toEqual({
53-
"one" : {"a" : true},
54-
"two" : {"b" : true}
52+
"a" : [one],
53+
"b" : [two]
5554
});
5655
});
5756

58-
it("should not include keys for controls that have no errors", () => {
59-
var g = new ControlGroup({
60-
"one" : new Control("one", validator("a", true)),
61-
"two" : new Control("one")
62-
});
57+
it("should not include controls that have no errors", () => {
58+
var one = new Control("one", validator("a", true));
59+
var two = new Control("two");
60+
var g = new ControlGroup({"one" : one, "two" : two});
6361

6462
expect(controlGroupValidator(g)).toEqual({
65-
"one" : {"a" : true}
63+
"a": [one]
6664
});
6765
});
6866

0 commit comments

Comments
 (0)