feat(forms): add the submitted flag to NgForm and NgFormModel directives

Closes #2960

Closes #7449
This commit is contained in:
Anton Moiseev 2015-12-06 22:21:53 +03:00 committed by Misko Hevery
parent 89f61087c7
commit 420e83a396
4 changed files with 61 additions and 0 deletions

View File

@ -86,6 +86,8 @@ export const formDirectiveProvider: any =
exportAs: 'ngForm'
})
export class NgForm extends ControlContainer implements Form {
private _submitted: boolean = false;
form: ControlGroup;
ngSubmit = new EventEmitter();
@ -96,6 +98,8 @@ export class NgForm extends ControlContainer implements Form {
composeAsyncValidators(asyncValidators));
}
get submitted(): boolean { return this._submitted; }
get formDirective(): Form { return this; }
get control(): ControlGroup { return this.form; }
@ -158,6 +162,7 @@ export class NgForm extends ControlContainer implements Form {
}
onSubmit(): boolean {
this._submitted = true;
ObservableWrapper.callEmit(this.ngSubmit, null);
return false;
}

View File

@ -107,6 +107,8 @@ export const formDirectiveProvider: any =
})
export class NgFormModel extends ControlContainer implements Form,
OnChanges {
private _submitted: boolean = false;
form: ControlGroup = null;
directives: NgControl[] = [];
ngSubmit = new EventEmitter();
@ -131,6 +133,8 @@ export class NgFormModel extends ControlContainer implements Form,
this._updateDomValue();
}
get submitted(): boolean { return this._submitted; }
get formDirective(): Form { return this; }
get control(): ControlGroup { return this.form; }
@ -166,6 +170,7 @@ export class NgFormModel extends ControlContainer implements Form,
}
onSubmit(): boolean {
this._submitted = true;
ObservableWrapper.callEmit(this.ngSubmit, null);
return false;
}

View File

@ -145,6 +145,55 @@ export function main() {
expect(fixture.debugElement.componentInstance.name).toEqual('updated');
})));
it("should mark NgForm as submitted on submit event",
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
var t = `<div>
<form #f="ngForm" (ngSubmit)="data=f.submitted"></form>
<span>{{data}}</span>
</div>`;
var fixture: ComponentFixture<MyComp8>;
tcb.overrideTemplate(MyComp8, t).createAsync(MyComp8).then(
(root) => { fixture = root; });
tick();
fixture.debugElement.componentInstance.data = false;
tick();
var form = fixture.debugElement.query(By.css("form"));
dispatchEvent(form.nativeElement, "submit");
tick();
expect(fixture.debugElement.componentInstance.data).toEqual(true);
})));
it("should mark NgFormModel as submitted on submit event",
inject([TestComponentBuilder], fakeAsync((tcb: TestComponentBuilder) => {
var t = `<div>
<form #f="ngForm" [ngFormModel]="form" (ngSubmit)="data=f.submitted"></form>
<span>{{data}}</span>
</div>`;
var fixture: ComponentFixture<MyComp8>;
tcb.overrideTemplate(MyComp8, t).createAsync(MyComp8).then(
(root) => { fixture = root; });
tick();
fixture.debugElement.componentInstance.form = new ControlGroup({});
fixture.debugElement.componentInstance.data = false;
tick();
var form = fixture.debugElement.query(By.css("form"));
dispatchEvent(form.nativeElement, "submit");
tick();
expect(fixture.debugElement.componentInstance.data).toEqual(true);
})));
it("should work with single controls",
inject([TestComponentBuilder, AsyncTestCompleter], (tcb: TestComponentBuilder, async) => {
var control = new Control("loginValue");

View File

@ -801,6 +801,7 @@ const COMMON = [
'NgForm.path:string[]',
'NgForm.removeControl(dir:NgControl):void',
'NgForm.removeControlGroup(dir:NgControlGroup):void',
'NgForm.submitted:boolean',
'NgForm.updateModel(dir:NgControl, value:any):void',
'NgFormControl',
'NgFormControl.asyncValidator:AsyncValidatorFn',
@ -830,6 +831,7 @@ const COMMON = [
'NgFormModel.path:string[]',
'NgFormModel.removeControl(dir:NgControl):void',
'NgFormModel.removeControlGroup(dir:NgControlGroup):any',
'NgFormModel.submitted:boolean',
'NgFormModel.updateModel(dir:NgControl, value:any):void',
'NgIf',
'NgIf.constructor(_viewContainer:ViewContainerRef, _templateRef:TemplateRef<Object>)',