Skip to content

Commit 8145c20

Browse files
committed
fix(grid): use column field names as names for the ngControls
1 parent 61addcd commit 8145c20

File tree

3 files changed

+59
-28
lines changed

3 files changed

+59
-28
lines changed

projects/igniteui-angular/src/lib/grids/cell.component.html

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,37 +226,38 @@
226226
}
227227

228228
<ng-template #defaultError>
229-
@if (formGroup?.get(getTransformedFieldName(column?.field)).errors?.['required']) {
229+
@let errors = formControl.errors;
230+
@if (errors?.['required']) {
230231
<div>
231232
{{grid.resourceStrings.igx_grid_required_validation_error}}
232233
</div>
233234
}
234-
@if (formGroup?.get(getTransformedFieldName(column?.field)).errors?.['minlength']) {
235+
@if (errors?.['minlength']) {
235236
<div>
236-
{{grid.resourceStrings.igx_grid_min_length_validation_error | igxStringReplace:'{0}':formGroup.get(getTransformedFieldName(column.field)).errors.minlength.requiredLength }}
237+
{{grid.resourceStrings.igx_grid_min_length_validation_error | igxStringReplace:'{0}':errors.minlength.requiredLength }}
237238
</div>
238239
}
239-
@if (formGroup?.get(getTransformedFieldName(column?.field)).errors?.['maxlength']) {
240+
@if (errors?.['maxlength']) {
240241
<div>
241-
{{grid.resourceStrings.igx_grid_max_length_validation_error | igxStringReplace:'{0}':formGroup.get(getTransformedFieldName(column.field)).errors.maxlength.requiredLength }}
242+
{{grid.resourceStrings.igx_grid_max_length_validation_error | igxStringReplace:'{0}':errors.maxlength.requiredLength }}
242243
</div>
243244
}
244-
@if (formGroup?.get(getTransformedFieldName(column?.field)).errors?.['min']) {
245+
@if (errors?.['min']) {
245246
<div>
246-
{{grid.resourceStrings.igx_grid_min_validation_error | igxStringReplace:'{0}':formGroup.get(getTransformedFieldName(column.field)).errors.min.min }}
247+
{{grid.resourceStrings.igx_grid_min_validation_error | igxStringReplace:'{0}':errors.min.min }}
247248
</div>
248249
}
249-
@if (formGroup?.get(getTransformedFieldName(column?.field)).errors?.['max']) {
250+
@if (errors?.['max']) {
250251
<div>
251-
{{grid.resourceStrings.igx_grid_max_validation_error | igxStringReplace:'{0}':formGroup.get(getTransformedFieldName(column.field)).errors.max.max }}
252+
{{grid.resourceStrings.igx_grid_max_validation_error | igxStringReplace:'{0}':errors.max.max }}
252253
</div>
253254
}
254-
@if (formGroup?.get(getTransformedFieldName(column?.field)).errors?.['email']) {
255+
@if (errors?.['email']) {
255256
<div>
256257
{{grid.resourceStrings.igx_grid_email_validation_error }}
257258
</div>
258259
}
259-
@if (formGroup?.get(getTransformedFieldName(column?.field)).errors?.['pattern']) {
260+
@if (errors?.['pattern']) {
260261
<div>
261262
{{grid.resourceStrings.igx_grid_pattern_validation_error}}
262263
</div>

projects/igniteui-angular/src/lib/grids/cell.component.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,9 +538,11 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy, CellT
538538
@HostBinding('class.igx-grid__td--invalid')
539539
@HostBinding('attr.aria-invalid')
540540
public get isInvalid() {
541-
const fieldName = this.getTransformedFieldName(this.column?.field);
542-
const isInvalid = this.formGroup?.get(fieldName)?.invalid && this.formGroup?.get(fieldName)?.touched;
543-
return !this.intRow.deleted && isInvalid;
541+
if (this.formGroup) {
542+
const isInvalid = this.grid.validation?.isFieldInvalid(this.formGroup, this.column?.field);
543+
return !this.intRow.deleted && isInvalid;
544+
}
545+
return false;
544546
}
545547

546548
/**
@@ -549,9 +551,11 @@ export class IgxGridCellComponent implements OnInit, OnChanges, OnDestroy, CellT
549551
*/
550552
@HostBinding('class.igx-grid__td--valid')
551553
public get isValidAfterEdit() {
552-
const fieldName = this.getTransformedFieldName(this.column?.field);
553-
const formControl = this.formGroup?.get(fieldName);
554-
return this.editMode && formControl && !formControl.invalid && formControl.dirty;
554+
if (this.formGroup) {
555+
const isValidAfterEdit = this.grid.validation?.isFieldValidAfterEdit(this.formGroup, this.column?.field);
556+
return this.editMode && isValidAfterEdit;
557+
}
558+
return false;
555559
}
556560

557561
/**

projects/igniteui-angular/src/lib/grids/grid/grid-validation.service.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)