@@ -59,10 +59,9 @@ export class IgxGridValidationService {
5959 */
6060 private addFormControl ( formGroup : FormGroup , data : any , column : ColumnType ) {
6161 const value = resolveNestedPath ( data || { } , columnFieldPath ( column . field ) ) ;
62- const field = this . getFieldKey ( column . field ) ;
6362 const control = new FormControl ( value , { updateOn : this . grid . validationTrigger } ) ;
6463 control . addValidators ( column . validators ) ;
65- formGroup . addControl ( field , control ) ;
64+ formGroup . addControl ( column . field , control ) ;
6665 control . setValue ( value ) ;
6766 }
6867
@@ -74,6 +73,17 @@ export class IgxGridValidationService {
7473 const parts = path ?. split ( '.' ) ?? [ ] ;
7574 return parts . join ( '_' ) ;
7675 }
76+
77+ /**
78+ * @hidden
79+ * @internal
80+ Wraps the provided path into an array. This way FormGroup.get will return proper result.
81+ Otherwise, if the path is a string (e.g. 'address.street'), FormGroup.get will treat it as there is a nested structure
82+ and will look for control with a name of 'address' which returns undefined.
83+ */
84+ private getFormControlPath ( path : string ) : ( string ) [ ] {
85+ return [ path ] ;
86+ }
7787
7888 /**
7989 * @hidden
@@ -89,8 +99,8 @@ export class IgxGridValidationService {
8999 */
90100 public getFormControl ( rowId : any , columnKey : string ) {
91101 const formControl = this . getFormGroup ( rowId ) ;
92- const field = this . getFieldKey ( columnKey ) ;
93- return formControl ?. get ( field ) ;
102+ const path = this . getFormControlPath ( columnKey ) ;
103+ return formControl ?. get ( path ) ;
94104 }
95105
96106 /**
@@ -101,6 +111,22 @@ export class IgxGridValidationService {
101111 this . _validityStates . set ( rowId , form ) ;
102112 }
103113
114+ /**
115+ * Checks the validity of the native ngControl
116+ */
117+ public isFieldInvalid ( formGroup : FormGroup , fieldName : string ) : boolean {
118+ const path = this . getFormControlPath ( fieldName ) ;
119+ return formGroup . get ( path ) ?. invalid && formGroup . get ( path ) ?. touched ;
120+ }
121+
122+ /**
123+ * Checks the validity of the native ngControl after edit
124+ */
125+ public isFieldValidAfterEdit ( formGroup : FormGroup , fieldName : string ) : boolean {
126+ const path = this . getFormControlPath ( fieldName ) ;
127+ return ! formGroup . get ( path ) ?. invalid && formGroup . get ( path ) ?. dirty ;
128+ }
129+
104130 /**
105131 * @hidden
106132 * @internal
@@ -110,10 +136,10 @@ export class IgxGridValidationService {
110136 this . _validityStates . forEach ( ( formGroup , key ) => {
111137 const state : IFieldValidationState [ ] = [ ] ;
112138 for ( const col of this . grid . columns ) {
113- const colKey = this . getFieldKey ( col . field ) ;
114- const control = formGroup . get ( colKey ) ;
139+ const path = this . getFormControlPath ( col . field ) ;
140+ const control = formGroup . get ( path ) ;
115141 if ( control ) {
116- state . push ( { field : colKey , status : control . status as ValidationStatus , errors : control . errors } )
142+ state . push ( { field : col . field , status : control . status as ValidationStatus , errors : control . errors } )
117143 }
118144 }
119145 states . push ( { key : key , status : formGroup . status as ValidationStatus , fields : state , errors : formGroup . errors } ) ;
@@ -138,8 +164,8 @@ export class IgxGridValidationService {
138164 const keys = Object . keys ( rowData ) ;
139165 const rowGroup = this . getFormGroup ( rowId ) ;
140166 for ( const key of keys ) {
141- const colKey = this . getFieldKey ( key ) ;
142- const control = rowGroup ?. get ( colKey ) ;
167+ const path = this . getFormControlPath ( key ) ;
168+ const control = rowGroup ?. get ( path ) ;
143169 if ( control && control . value !== rowData [ key ] ) {
144170 control . setValue ( rowData [ key ] , { emitEvent : false } ) ;
145171 }
@@ -174,8 +200,8 @@ export class IgxGridValidationService {
174200 rowGroup . markAsTouched ( ) ;
175201 const fields = field ? [ field ] : this . grid . columns . map ( x => x . field ) ;
176202 for ( const currField of fields ) {
177- const colKey = this . getFieldKey ( currField ) ;
178- rowGroup ?. get ( colKey ) ?. markAsTouched ( ) ;
203+ const path = this . getFormControlPath ( currField ) ;
204+ rowGroup ?. get ( path ) ?. markAsTouched ( ) ;
179205 }
180206 }
181207
0 commit comments