clean(forms): cleanup
This commit is contained in:
parent
f27e538e2c
commit
cf9cb61665
@ -68,11 +68,8 @@ class StringMapWrapper {
|
|||||||
m.forEach((k, v) => fn(v, k));
|
m.forEach((k, v) => fn(v, k));
|
||||||
}
|
}
|
||||||
static HashMap merge(HashMap a, HashMap b) {
|
static HashMap merge(HashMap a, HashMap b) {
|
||||||
var m = {};
|
var m = new HashMap.from(a);
|
||||||
|
|
||||||
a.forEach((k, v) => m[k] = v);
|
|
||||||
b.forEach((k, v) => m[k] = v);
|
b.forEach((k, v) => m[k] = v);
|
||||||
|
|
||||||
return m;
|
return m;
|
||||||
}
|
}
|
||||||
static bool isEmpty(Map m) => m.isEmpty;
|
static bool isEmpty(Map m) => m.isEmpty;
|
||||||
|
21
modules/angular2/src/forms/model.js
vendored
21
modules/angular2/src/forms/model.js
vendored
@ -9,6 +9,7 @@ export const INVALID = "INVALID";
|
|||||||
// get value():any;
|
// get value():any;
|
||||||
// validator:Function;
|
// validator:Function;
|
||||||
// get status():string;
|
// get status():string;
|
||||||
|
// get valid():boolean;
|
||||||
// get errors():Map;
|
// get errors():Map;
|
||||||
// get active():boolean {}
|
// get active():boolean {}
|
||||||
// updateValue(value:any){}
|
// updateValue(value:any){}
|
||||||
@ -19,19 +20,19 @@ export class Control {
|
|||||||
_value:any;
|
_value:any;
|
||||||
_status:string;
|
_status:string;
|
||||||
_errors;
|
_errors;
|
||||||
_updated:boolean;
|
_dirty:boolean;
|
||||||
_parent:ControlGroup;
|
_parent:ControlGroup;
|
||||||
validator:Function;
|
validator:Function;
|
||||||
|
|
||||||
constructor(value:any, validator:Function = nullValidator) {
|
constructor(value:any, validator:Function = nullValidator) {
|
||||||
this._value = value;
|
this._value = value;
|
||||||
this.validator = validator;
|
this.validator = validator;
|
||||||
this._updated = true;
|
this._dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
updateValue(value:any) {
|
updateValue(value:any) {
|
||||||
this._value = value;
|
this._value = value;
|
||||||
this._updated = true;
|
this._dirty = true;
|
||||||
this._updateParent();
|
this._updateParent();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -63,8 +64,8 @@ export class Control {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_updateIfNeeded() {
|
_updateIfNeeded() {
|
||||||
if (this._updated) {
|
if (this._dirty) {
|
||||||
this._updated = false;
|
this._dirty = false;
|
||||||
this._errors = this.validator(this);
|
this._errors = this.validator(this);
|
||||||
this._status = isPresent(this._errors) ? INVALID : VALID;
|
this._status = isPresent(this._errors) ? INVALID : VALID;
|
||||||
}
|
}
|
||||||
@ -81,14 +82,14 @@ export class ControlGroup {
|
|||||||
_value:any;
|
_value:any;
|
||||||
_status:string;
|
_status:string;
|
||||||
_errors;
|
_errors;
|
||||||
_updated:boolean;
|
_dirty:boolean;
|
||||||
validator:Function;
|
validator:Function;
|
||||||
controls;
|
controls;
|
||||||
|
|
||||||
constructor(controls, validator:Function = controlGroupValidator) {
|
constructor(controls, validator:Function = controlGroupValidator) {
|
||||||
this.controls = controls;
|
this.controls = controls;
|
||||||
this.validator = validator;
|
this.validator = validator;
|
||||||
this._updated = true;
|
this._dirty = true;
|
||||||
this._setParentForControls();
|
this._setParentForControls();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,8 +120,8 @@ export class ControlGroup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_updateIfNeeded() {
|
_updateIfNeeded() {
|
||||||
if (this._updated) {
|
if (this._dirty) {
|
||||||
this._updated = false;
|
this._dirty = false;
|
||||||
this._value = this._reduceValue();
|
this._value = this._reduceValue();
|
||||||
this._errors = this.validator(this);
|
this._errors = this.validator(this);
|
||||||
this._status = isPresent(this._errors) ? INVALID : VALID;
|
this._status = isPresent(this._errors) ? INVALID : VALID;
|
||||||
@ -138,7 +139,7 @@ export class ControlGroup {
|
|||||||
}
|
}
|
||||||
|
|
||||||
_controlChanged() {
|
_controlChanged() {
|
||||||
this._updated = true;
|
this._dirty = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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 {Decorator} from 'angular2/core';
|
||||||
|
|
||||||
import {ControlGroup, Control, ControlDirective} from 'angular2/forms';
|
import {ControlDirective} from 'angular2/forms';
|
||||||
import * as validators from 'angular2/forms';
|
import * as validators from 'angular2/forms';
|
||||||
|
|
||||||
@Decorator({
|
@Decorator({
|
||||||
|
7
modules/angular2/src/forms/validators.js
vendored
7
modules/angular2/src/forms/validators.js
vendored
@ -25,7 +25,12 @@ export function controlGroupValidator(c:ControlGroup) {
|
|||||||
var res = {};
|
var res = {};
|
||||||
StringMapWrapper.forEach(c.controls, (control, name) => {
|
StringMapWrapper.forEach(c.controls, (control, name) => {
|
||||||
if (control.active && isPresent(control.errors)) {
|
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;
|
return StringMapWrapper.isEmpty(res) ? null : res;
|
||||||
|
7
modules/angular2/test/forms/model_spec.js
vendored
7
modules/angular2/test/forms/model_spec.js
vendored
@ -51,13 +51,12 @@ export function main() {
|
|||||||
});
|
});
|
||||||
|
|
||||||
it("should run the validator with the initial value (invalid)", () => {
|
it("should run the validator with the initial value (invalid)", () => {
|
||||||
var g = new ControlGroup({
|
var one = new Control(null, validations.required);
|
||||||
"one": new Control(null, validations.required)
|
var g = new ControlGroup({"one": one});
|
||||||
});
|
|
||||||
|
|
||||||
expect(g.valid).toEqual(false);
|
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", () => {
|
it("should run the validator with the value changes", () => {
|
||||||
|
22
modules/angular2/test/forms/validators_spec.js
vendored
22
modules/angular2/test/forms/validators_spec.js
vendored
@ -44,25 +44,23 @@ export function main() {
|
|||||||
|
|
||||||
describe("controlGroupValidator", () => {
|
describe("controlGroupValidator", () => {
|
||||||
it("should collect errors from the child controls", () => {
|
it("should collect errors from the child controls", () => {
|
||||||
var g = new ControlGroup({
|
var one = new Control("one", validator("a", true));
|
||||||
"one" : new Control("one", validator("a", true)),
|
var two = new Control("one", validator("b", true));
|
||||||
"two" : new Control("two", validator("b", true))
|
var g = new ControlGroup({"one" : one, "two" : two});
|
||||||
});
|
|
||||||
|
|
||||||
expect(controlGroupValidator(g)).toEqual({
|
expect(controlGroupValidator(g)).toEqual({
|
||||||
"one" : {"a" : true},
|
"a" : [one],
|
||||||
"two" : {"b" : true}
|
"b" : [two]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should not include keys for controls that have no errors", () => {
|
it("should not include controls that have no errors", () => {
|
||||||
var g = new ControlGroup({
|
var one = new Control("one", validator("a", true));
|
||||||
"one" : new Control("one", validator("a", true)),
|
var two = new Control("two");
|
||||||
"two" : new Control("one")
|
var g = new ControlGroup({"one" : one, "two" : two});
|
||||||
});
|
|
||||||
|
|
||||||
expect(controlGroupValidator(g)).toEqual({
|
expect(controlGroupValidator(g)).toEqual({
|
||||||
"one" : {"a" : true}
|
"a": [one]
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user