Skip to content

Commit 10fb7bb

Browse files
committed
refactor(forms): make form group responsible for supporting optional controls
1 parent 5acde20 commit 10fb7bb

4 files changed

Lines changed: 72 additions & 94 deletions

File tree

modules/angular2/forms.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './src/forms/model';
22
export * from './src/forms/directives';
33
export * from './src/forms/validators';
4-
export * from './src/forms/validator_directives';
4+
export * from './src/forms/validator_directives';
5+
export * from './src/forms/form_builder';

modules/angular2/src/forms/model.js

Lines changed: 22 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export const INVALID = "INVALID";
1111
// get status():string;
1212
// get valid():boolean;
1313
// get errors():Map;
14-
// get active():boolean {}
1514
// updateValue(value:any){}
1615
// setParent(parent){}
1716
//}
@@ -29,10 +28,6 @@ export class AbstractControl {
2928
this._dirty = true;
3029
}
3130

32-
get active():boolean {
33-
return true;
34-
}
35-
3631
get value() {
3732
this._updateIfNeeded();
3833
return this._value;
@@ -90,13 +85,30 @@ export class Control extends AbstractControl {
9085

9186
export class ControlGroup extends AbstractControl {
9287
controls;
88+
optionals;
9389

94-
constructor(controls, validator:Function = controlGroupValidator) {
90+
constructor(controls, optionals = null, validator:Function = controlGroupValidator) {
9591
super(validator);
9692
this.controls = controls;
93+
this.optionals = isPresent(optionals) ? optionals : {};
9794
this._setParentForControls();
9895
}
9996

97+
include(controlName:string) {
98+
this._dirty = true;
99+
StringMapWrapper.set(this.optionals, controlName, true);
100+
}
101+
102+
exclude(controlName:string) {
103+
this._dirty = true;
104+
StringMapWrapper.set(this.optionals, controlName, false);
105+
}
106+
107+
contains(controlName:string) {
108+
var c = StringMapWrapper.contains(this.controls, controlName);
109+
return c && this._included(controlName);
110+
}
111+
100112
_setParentForControls() {
101113
StringMapWrapper.forEach(this.controls, (control, name) => {
102114
control.setParent(this);
@@ -115,7 +127,7 @@ export class ControlGroup extends AbstractControl {
115127
_reduceValue() {
116128
var newValue = {};
117129
StringMapWrapper.forEach(this.controls, (control, name) => {
118-
if (control.active) {
130+
if (this._included(name)) {
119131
newValue[name] = control.value;
120132
}
121133
});
@@ -126,56 +138,9 @@ export class ControlGroup extends AbstractControl {
126138
this._dirty = true;
127139
this._updateParent();
128140
}
129-
}
130-
131-
export class OptionalControl {
132-
_control:Control;
133-
_cond:boolean;
134-
135-
constructor(control:Control, cond:boolean) {
136-
super();
137-
this._control = control;
138-
this._cond = cond;
139-
}
140-
141-
get active():boolean {
142-
return this._cond;
143-
}
144-
145-
get value() {
146-
return this._control.value;
147-
}
148-
149-
get status() {
150-
return this._control.status;
151-
}
152-
153-
get errors() {
154-
return this._control.errors;
155-
}
156-
157-
set validator(v) {
158-
this._control.validator = v;
159-
}
160141

161-
get validator() {
162-
return this._control.validator;
163-
}
164-
165-
set cond(value:boolean){
166-
this._cond = value;
167-
this._control._updateParent();
168-
}
169-
170-
get cond():boolean{
171-
return this._cond;
172-
}
173-
174-
updateValue(value:any){
175-
this._control.updateValue(value);
176-
}
177-
178-
setParent(parent){
179-
this._control.setParent(parent);
142+
_included(controlName:string):boolean {
143+
var isOptional = StringMapWrapper.contains(this.optionals, controlName);
144+
return !isOptional || StringMapWrapper.get(this.optionals, controlName);
180145
}
181146
}

modules/angular2/src/forms/validators.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export function compose(validators:List<Function>):Function {
2424
export function controlGroupValidator(c:ControlGroup) {
2525
var res = {};
2626
StringMapWrapper.forEach(c.controls, (control, name) => {
27-
if (control.active && isPresent(control.errors)) {
27+
if (c.contains(name) && isPresent(control.errors)) {
2828
StringMapWrapper.forEach(control.errors, (value, error) => {
2929
if (! StringMapWrapper.contains(res, error)) {
3030
res[error] = [];

modules/angular2/test/forms/model_spec.js

Lines changed: 47 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -84,56 +84,68 @@ export function main() {
8484
expect(g.errors).toEqual(null);
8585
});
8686
});
87-
});
88-
89-
describe("OptionalControl", () => {
90-
it("should read properties from the wrapped component", () => {
91-
var wrapperControl = new Control("value", validations.required);
92-
var c = new OptionalControl(wrapperControl, true);
9387

94-
expect(c.value).toEqual('value');
95-
expect(c.status).toEqual('VALID');
96-
expect(c.validator).toEqual(validations.required);
97-
});
88+
describe("optional components", () => {
89+
describe("contains", () => {
90+
var group;
91+
92+
beforeEach(() => {
93+
group = new ControlGroup({
94+
"required": new Control("requiredValue"),
95+
"optional": new Control("optionalValue")
96+
}, {
97+
"optional": false
98+
});
99+
});
98100

99-
it("should update the wrapped component", () => {
100-
var wrappedControl = new Control("value");
101-
var c = new OptionalControl(wrappedControl, true);
101+
// rename contains into has
102+
it("should return false when the component is not included", () => {
103+
expect(group.contains("optional")).toEqual(false);
104+
})
102105

103-
c.validator = validations.required;
104-
c.updateValue("newValue");
106+
it("should return false when there is no component with the given name", () => {
107+
expect(group.contains("something else")).toEqual(false);
108+
});
105109

110+
it("should return true when the component is included", () => {
111+
expect(group.contains("required")).toEqual(true);
106112

107-
expect(wrappedControl.validator).toEqual(validations.required);
108-
expect(wrappedControl.value).toEqual('newValue');
109-
});
113+
group.include("optional");
110114

111-
it("should not include an inactive component into the group value", () => {
112-
var group = new ControlGroup({
113-
"required" : new Control("requiredValue"),
114-
"optional" : new OptionalControl(new Control("optionalValue"), false)
115+
expect(group.contains("optional")).toEqual(true);
116+
});
115117
});
116118

117-
expect(group.value).toEqual({"required" : "requiredValue"});
119+
it("should not include an inactive component into the group value", () => {
120+
var group = new ControlGroup({
121+
"required": new Control("requiredValue"),
122+
"optional": new Control("optionalValue")
123+
}, {
124+
"optional": false
125+
});
118126

119-
group.controls["optional"].cond = true;
127+
expect(group.value).toEqual({"required" : "requiredValue"});
120128

121-
expect(group.value).toEqual({"required" : "requiredValue", "optional" : "optionalValue"});
122-
});
129+
group.include("optional");
123130

124-
it("should not run validations on an inactive component", () => {
125-
var group = new ControlGroup({
126-
"required" : new Control("requiredValue", validations.required),
127-
"optional" : new OptionalControl(new Control("", validations.required), false)
131+
expect(group.value).toEqual({"required" : "requiredValue", "optional" : "optionalValue"});
128132
});
129133

130-
expect(group.valid).toEqual(true);
134+
it("should not run validations on an inactive component", () => {
135+
var group = new ControlGroup({
136+
"required": new Control("requiredValue", validations.required),
137+
"optional": new Control("", validations.required)
138+
}, {
139+
"optional": false
140+
});
141+
142+
expect(group.valid).toEqual(true);
131143

132-
group.controls["optional"].cond = true;
144+
group.include("optional");
133145

134-
expect(group.valid).toEqual(false);
146+
expect(group.valid).toEqual(false);
147+
});
135148
});
136149
});
137-
138150
});
139-
}
151+
}

0 commit comments

Comments
 (0)