From 420e83a396ed5aee0c95a09c5d0877b956568c0f Mon Sep 17 00:00:00 2001 From: Anton Moiseev Date: Sun, 6 Dec 2015 22:21:53 +0300 Subject: [PATCH] feat(forms): add the submitted flag to NgForm and NgFormModel directives Closes #2960 Closes #7449 --- .../common/src/forms/directives/ng_form.ts | 5 ++ .../src/forms/directives/ng_form_model.ts | 5 ++ .../common/test/forms/integration_spec.ts | 49 +++++++++++++++++++ tools/public_api_guard/public_api_spec.ts | 2 + 4 files changed, 61 insertions(+) diff --git a/modules/@angular/common/src/forms/directives/ng_form.ts b/modules/@angular/common/src/forms/directives/ng_form.ts index 704d9178b1..8109f88a20 100644 --- a/modules/@angular/common/src/forms/directives/ng_form.ts +++ b/modules/@angular/common/src/forms/directives/ng_form.ts @@ -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; } diff --git a/modules/@angular/common/src/forms/directives/ng_form_model.ts b/modules/@angular/common/src/forms/directives/ng_form_model.ts index 8b654161e0..6067ea020e 100644 --- a/modules/@angular/common/src/forms/directives/ng_form_model.ts +++ b/modules/@angular/common/src/forms/directives/ng_form_model.ts @@ -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; } diff --git a/modules/@angular/common/test/forms/integration_spec.ts b/modules/@angular/common/test/forms/integration_spec.ts index 371f87f813..3a65372c55 100644 --- a/modules/@angular/common/test/forms/integration_spec.ts +++ b/modules/@angular/common/test/forms/integration_spec.ts @@ -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 = `
+
+ {{data}} +
`; + + var fixture: ComponentFixture; + + 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 = `
+
+ {{data}} +
`; + + var fixture: ComponentFixture; + + 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"); diff --git a/tools/public_api_guard/public_api_spec.ts b/tools/public_api_guard/public_api_spec.ts index 9460855c9a..c2be6953b3 100644 --- a/tools/public_api_guard/public_api_spec.ts +++ b/tools/public_api_guard/public_api_spec.ts @@ -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)',