11import { Component , ContentChildren , QueryList , Input , ViewEncapsulation , AfterContentInit ,
2- OnDestroy , Optional , OnInit , Inject } from '@angular/core' ;
2+ OnDestroy , Optional , Inject } from '@angular/core' ;
33import { FormControl , ControlContainer } from '@angular/forms' ;
44import { Subscription } from 'rxjs' ;
55
@@ -23,18 +23,25 @@ import { ReactiveValidationModuleConfigurationToken } from '../reactive-validati
2323 * messages specified within the reactive form model, or shows custom messages declared using the
2424 * ValidationMessageComponent.
2525 */
26- export class ValidationMessagesComponent implements OnInit , AfterContentInit , OnDestroy {
26+ export class ValidationMessagesComponent implements AfterContentInit , OnDestroy {
2727 private _for : FormControl [ ] = [ ] ;
2828 private messageComponentChangesContainer : ObservableContainer < QueryList < ValidationMessageComponent > > =
2929 new ObservableContainer ( ( ) => this . validateChildren ( ) ) ;
3030 private controlStatusChangesContainer : ObservableContainer < FormControl > =
3131 new ObservableContainer ( executeAfterContentInit ( item => this . handleControlStatusChange ( item ) , this ) ) ;
3232
33- private formSubmitted = false ;
33+ private formSubmitted : boolean | undefined = undefined ;
3434 private formSubmittedSubscription : Subscription ;
3535
36- constructor ( @Optional ( ) private controlContainer : ControlContainer , @Optional ( ) private formSubmitDirective : FormDirective ,
37- @Optional ( ) @Inject ( ReactiveValidationModuleConfigurationToken ) private configuration : ReactiveValidationModuleConfiguration ) { }
36+ constructor ( @Optional ( ) private controlContainer : ControlContainer , @Optional ( ) formSubmitDirective : FormDirective ,
37+ @Optional ( ) @Inject ( ReactiveValidationModuleConfigurationToken ) private configuration : ReactiveValidationModuleConfiguration ) {
38+ if ( formSubmitDirective ) {
39+ this . formSubmitted = false ;
40+ this . formSubmittedSubscription = formSubmitDirective . submitted . subscribe ( ( ) => {
41+ this . formSubmitted = true ;
42+ } ) ;
43+ }
44+ }
3845
3946 @ContentChildren ( ValidationMessageComponent ) private messageComponents : QueryList < ValidationMessageComponent > ;
4047
@@ -63,14 +70,6 @@ export class ValidationMessagesComponent implements OnInit, AfterContentInit, On
6370 this . controlStatusChangesContainer . subscribe ( this . _for , control => control . statusChanges , true ) ;
6471 }
6572
66- ngOnInit ( ) {
67- if ( this . formSubmitDirective ) {
68- this . formSubmittedSubscription = this . formSubmitDirective . submitted . subscribe ( ( ) => {
69- this . formSubmitted = true ;
70- } ) ;
71- }
72- }
73-
7473 ngAfterContentInit ( ) {
7574 this . messageComponentChangesContainer . subscribe ( this . messageComponents , queryList => queryList . changes , true ) ;
7675 }
@@ -94,8 +93,7 @@ export class ValidationMessagesComponent implements OnInit, AfterContentInit, On
9493
9594 private getFirstErrorPerControl ( ) {
9695 return < ValidationError [ ] > this . _for . filter ( control => this . configuration && this . configuration . displayValidationMessageWhen ?
97- this . configuration . displayValidationMessageWhen ( control , this . formSubmitDirective ? this . formSubmitted : undefined ) :
98- control . touched || this . formSubmitted
96+ this . configuration . displayValidationMessageWhen ( control , this . formSubmitted ) : control . touched || this . formSubmitted
9997 ) . map ( ValidationError . fromFirstError ) . filter ( value => value !== undefined ) ;
10098 }
10199
0 commit comments