@@ -59,20 +59,21 @@ export class IgxGridValidationService {
59
59
*/
60
60
private addFormControl ( formGroup : FormGroup , data : any , column : ColumnType ) {
61
61
const value = resolveNestedPath ( data || { } , columnFieldPath ( column . field ) ) ;
62
- const field = this . getFieldKey ( column . field ) ;
63
62
const control = new FormControl ( value , { updateOn : this . grid . validationTrigger } ) ;
64
63
control . addValidators ( column . validators ) ;
65
- formGroup . addControl ( field , control ) ;
64
+ formGroup . addControl ( column . field , control ) ;
66
65
control . setValue ( value ) ;
67
66
}
68
-
67
+
69
68
/**
70
69
* @hidden
71
70
* @internal
71
+ Wraps the provided path into an array. This way FormGroup.get will return proper result.
72
+ Otherwise, if the path is a string (e.g. 'address.street'), FormGroup.get will treat it as there is a nested structure
73
+ and will look for control with a name of 'address' which returns undefined.
72
74
*/
73
- private getFieldKey ( path : string ) {
74
- const parts = path ?. split ( '.' ) ?? [ ] ;
75
- return parts . join ( '_' ) ;
75
+ private getFormControlPath ( path : string ) : ( string ) [ ] {
76
+ return [ path ] ;
76
77
}
77
78
78
79
/**
@@ -89,8 +90,8 @@ export class IgxGridValidationService {
89
90
*/
90
91
public getFormControl ( rowId : any , columnKey : string ) {
91
92
const formControl = this . getFormGroup ( rowId ) ;
92
- const field = this . getFieldKey ( columnKey ) ;
93
- return formControl ?. get ( field ) ;
93
+ const path = this . getFormControlPath ( columnKey ) ;
94
+ return formControl ?. get ( path ) ;
94
95
}
95
96
96
97
/**
@@ -101,6 +102,22 @@ export class IgxGridValidationService {
101
102
this . _validityStates . set ( rowId , form ) ;
102
103
}
103
104
105
+ /**
106
+ * Checks the validity of the native ngControl
107
+ */
108
+ public isFieldInvalid ( formGroup : FormGroup , fieldName : string ) : boolean {
109
+ const path = this . getFormControlPath ( fieldName ) ;
110
+ return formGroup . get ( path ) ?. invalid && formGroup . get ( path ) ?. touched ;
111
+ }
112
+
113
+ /**
114
+ * Checks the validity of the native ngControl after edit
115
+ */
116
+ public isFieldValidAfterEdit ( formGroup : FormGroup , fieldName : string ) : boolean {
117
+ const path = this . getFormControlPath ( fieldName ) ;
118
+ return ! formGroup . get ( path ) ?. invalid && formGroup . get ( path ) ?. dirty ;
119
+ }
120
+
104
121
/**
105
122
* @hidden
106
123
* @internal
@@ -110,10 +127,10 @@ export class IgxGridValidationService {
110
127
this . _validityStates . forEach ( ( formGroup , key ) => {
111
128
const state : IFieldValidationState [ ] = [ ] ;
112
129
for ( const col of this . grid . columns ) {
113
- const colKey = this . getFieldKey ( col . field ) ;
114
- const control = formGroup . get ( colKey ) ;
130
+ const path = this . getFormControlPath ( col . field ) ;
131
+ const control = formGroup . get ( path ) ;
115
132
if ( control ) {
116
- state . push ( { field : colKey , status : control . status as ValidationStatus , errors : control . errors } )
133
+ state . push ( { field : col . field , status : control . status as ValidationStatus , errors : control . errors } )
117
134
}
118
135
}
119
136
states . push ( { key : key , status : formGroup . status as ValidationStatus , fields : state , errors : formGroup . errors } ) ;
@@ -138,8 +155,8 @@ export class IgxGridValidationService {
138
155
const keys = Object . keys ( rowData ) ;
139
156
const rowGroup = this . getFormGroup ( rowId ) ;
140
157
for ( const key of keys ) {
141
- const colKey = this . getFieldKey ( key ) ;
142
- const control = rowGroup ?. get ( colKey ) ;
158
+ const path = this . getFormControlPath ( key ) ;
159
+ const control = rowGroup ?. get ( path ) ;
143
160
if ( control && control . value !== rowData [ key ] ) {
144
161
control . setValue ( rowData [ key ] , { emitEvent : false } ) ;
145
162
}
@@ -174,8 +191,8 @@ export class IgxGridValidationService {
174
191
rowGroup . markAsTouched ( ) ;
175
192
const fields = field ? [ field ] : this . grid . columns . map ( x => x . field ) ;
176
193
for ( const currField of fields ) {
177
- const colKey = this . getFieldKey ( currField ) ;
178
- rowGroup ?. get ( colKey ) ?. markAsTouched ( ) ;
194
+ const path = this . getFormControlPath ( currField ) ;
195
+ rowGroup ?. get ( path ) ?. markAsTouched ( ) ;
179
196
}
180
197
}
181
198
0 commit comments