Skip to content

Commit

Permalink
fix(forms): improve error message when ngFormModel is missing a form
Browse files Browse the repository at this point in the history
  • Loading branch information
kara committed Apr 20, 2016
1 parent 43e31c5 commit 24575e4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
11 changes: 10 additions & 1 deletion modules/angular2/src/common/forms/directives/ng_form_model.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {CONST_EXPR} from 'angular2/src/facade/lang';
import {CONST_EXPR, isBlank} from 'angular2/src/facade/lang';
import {ListWrapper, StringMapWrapper} from 'angular2/src/facade/collection';
import {BaseException} from 'angular2/src/facade/exceptions';
import {ObservableWrapper, EventEmitter} from 'angular2/src/facade/async';
import {
SimpleChange,
Expand Down Expand Up @@ -114,6 +115,7 @@ export class NgFormModel extends ControlContainer implements Form,
}

ngOnChanges(changes: {[key: string]: SimpleChange}): void {
this._checkFormPresent();
if (StringMapWrapper.contains(changes, "form")) {
var sync = composeValidators(this._validators);
this.form.validator = Validators.compose([this.form.validator, sync]);
Expand Down Expand Up @@ -173,4 +175,11 @@ export class NgFormModel extends ControlContainer implements Form,
dir.valueAccessor.writeValue(ctrl.value);
});
}

private _checkFormPresent() {
if (isBlank(this.form)) {
throw new BaseException(
`ngFormModel expects a form. Please pass one in. Example: <form [ngFormModel]="myCoolForm">`);
}
}
}
13 changes: 13 additions & 0 deletions modules/angular2/test/common/forms/integration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ export function main() {
});
}));

it("should throw if a form isn't passed into ngFormModel",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var t = `<div [ngFormModel]="form">
<input type="text" ngControl="login">
</div>`;

tcb.overrideTemplate(MyComp, t).createAsync(MyComp).then((fixture) => {
expect(() => fixture.detectChanges())
.toThrowError(new RegExp(`ngFormModel expects a form. Please pass one in.`));
async.done();
});
}));

it("should update the control group values on DOM change",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var form = new ControlGroup({"login": new Control("oldValue")});
Expand Down

0 comments on commit 24575e4

Please sign in to comment.