Skip to content

Commit 11e4385

Browse files
vsavkinmhevery
authored andcommitted
feat(forms): improved error messages
Closes angular#1839
1 parent ad29b12 commit 11e4385

File tree

3 files changed

+62
-32
lines changed

3 files changed

+62
-32
lines changed

modules/angular2/src/forms/directives.js

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
1-
import {Directive, onChange} from 'angular2/src/core/annotations_impl/annotations';
1+
import {Directive} from 'angular2/src/core/annotations_impl/annotations';
22
import {Ancestor} from 'angular2/src/core/annotations_impl/visibility';
33
import {ElementRef} from 'angular2/src/core/compiler/element_ref';
44
import {Optional} from 'angular2/src/di/annotations_impl';
55
import {Renderer} from 'angular2/src/render/api';
6-
import {isPresent, isString, CONST_EXPR} from 'angular2/src/facade/lang';
6+
import {isPresent, isString, CONST_EXPR, isBlank, BaseException} from 'angular2/src/facade/lang';
77
import {ListWrapper} from 'angular2/src/facade/collection';
8-
import {ControlGroup} from './model';
8+
import {ControlGroup, Control, isControl} from './model';
99
import {Validators} from './validators';
1010

1111
//export interface ControlValueAccessor {
1212
// writeValue(value):void{}
1313
// set onChange(fn){}
1414
//}
1515

16+
function _lookupControl(groupDirective:ControlGroupDirective, controlOrName:any):any {
17+
if (isControl(controlOrName)) {
18+
return controlOrName;
19+
}
20+
21+
if (isBlank(groupDirective)) {
22+
throw new BaseException(`No control group found for "${controlOrName}"`);
23+
}
24+
25+
var control = groupDirective.findControl(controlOrName);
26+
27+
if (isBlank(control)) {
28+
throw new BaseException(`Cannot find control "${controlOrName}"`);
29+
}
30+
31+
return control;
32+
}
33+
1634
/**
1735
* The default accessor for writing a value and listening to changes that is used by a {@link Control} directive.
1836
*
@@ -119,7 +137,6 @@ export class CheckboxControlValueAccessor {
119137
* @exportedAs angular2/forms
120138
*/
121139
@Directive({
122-
lifecycle: [onChange],
123140
selector: '[control]',
124141
properties: {
125142
'controlOrName' : 'control'
@@ -128,25 +145,21 @@ export class CheckboxControlValueAccessor {
128145
export class ControlDirective {
129146
_groupDirective:ControlGroupDirective;
130147

131-
controlOrName:any;
148+
_controlOrName:any;
132149
valueAccessor:any; //ControlValueAccessor
133150

134151
validator:Function;
135152

136153
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective, valueAccessor:DefaultValueAccessor) {
137154
this._groupDirective = groupDirective;
138-
this.controlOrName = null;
155+
this._controlOrName = null;
139156
this.valueAccessor = valueAccessor;
140157
this.validator = Validators.nullValidator;
141158
}
142159

143-
// TODO: vsavkin this should be moved into the constructor once static bindings
144-
// are implemented
145-
onChange(_) {
146-
this._initialize();
147-
}
160+
set controlOrName(controlOrName) {
161+
this._controlOrName = controlOrName;
148162

149-
_initialize() {
150163
if(isPresent(this._groupDirective)) {
151164
this._groupDirective.addDirective(this);
152165
}
@@ -167,11 +180,7 @@ export class ControlDirective {
167180
}
168181

169182
_control() {
170-
if (isString(this.controlOrName)) {
171-
return this._groupDirective.findControl(this.controlOrName);
172-
} else {
173-
return this.controlOrName;
174-
}
183+
return _lookupControl(this._groupDirective, this._controlOrName);
175184
}
176185
}
177186

@@ -218,27 +227,21 @@ export class ControlDirective {
218227
@Directive({
219228
selector: '[control-group]',
220229
properties: {
221-
'controlGroup' : 'control-group'
230+
'controlOrName' : 'control-group'
222231
}
223232
})
224233
export class ControlGroupDirective {
225234
_groupDirective:ControlGroupDirective;
226-
_controlGroupName:string;
227-
228-
_controlGroup:ControlGroup;
229235
_directives:List<ControlDirective>;
236+
_controlOrName:any;
230237

231238
constructor(@Optional() @Ancestor() groupDirective:ControlGroupDirective) {
232239
this._groupDirective = groupDirective;
233240
this._directives = ListWrapper.create();
234241
}
235242

236-
set controlGroup(controlGroup) {
237-
if (isString(controlGroup)) {
238-
this._controlGroupName = controlGroup;
239-
} else {
240-
this._controlGroup = controlGroup;
241-
}
243+
set controlOrName(controlOrName) {
244+
this._controlOrName = controlOrName;
242245
this._updateDomValue();
243246
}
244247

@@ -255,11 +258,7 @@ export class ControlGroupDirective {
255258
}
256259

257260
_getControlGroup():ControlGroup {
258-
if (isPresent(this._controlGroupName)) {
259-
return this._groupDirective.findControl(this._controlGroupName)
260-
} else {
261-
return this._controlGroup;
262-
}
261+
return _lookupControl(this._groupDirective, this._controlOrName);
263262
}
264263
}
265264

modules/angular2/src/forms/model.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ export const INVALID = "INVALID";
2929
// setParent(parent){}
3030
//}
3131

32+
export function isControl(c:Object):boolean {
33+
return c instanceof AbstractControl;
34+
}
35+
36+
3237
/**
3338
* Omitting from external API doc as this is really an abstract internal concept.
3439
*/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import {ddescribe, describe, it, iit, xit, expect, beforeEach, afterEach, el,
2+
AsyncTestCompleter, inject} from 'angular2/test_lib';
3+
import {ControlGroup, ControlDirective, ControlGroupDirective} from 'angular2/forms';
4+
5+
export function main() {
6+
describe("Form Directives", () => {
7+
describe("Control", () => {
8+
it("should throw when the group is not found and the control is not set", () => {
9+
var c = new ControlDirective(null, null);
10+
expect(() => {
11+
c.controlOrName = 'login';
12+
}).toThrowError(new RegExp('No control group found for "login"'));
13+
});
14+
15+
it("should throw when cannot find the control in the group", () => {
16+
var emptyGroup = new ControlGroupDirective(null);
17+
emptyGroup.controlOrName = new ControlGroup({});
18+
19+
var c = new ControlDirective(emptyGroup, null);
20+
expect(() => {
21+
c.controlOrName = 'login';
22+
}).toThrowError(new RegExp('Cannot find control "login"'));
23+
});
24+
});
25+
});
26+
}

0 commit comments

Comments
 (0)