• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

IgniteUI / igniteui-angular / 13331632524

14 Feb 2025 02:51PM CUT coverage: 22.015% (-69.6%) from 91.622%
13331632524

Pull #15372

github

web-flow
Merge d52d57714 into bcb78ae0a
Pull Request #15372: chore(*): test ci passing

1990 of 15592 branches covered (12.76%)

431 of 964 new or added lines in 18 files covered. (44.71%)

19956 existing lines in 307 files now uncovered.

6452 of 29307 relevant lines covered (22.02%)

249.17 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

6.85
/projects/igniteui-angular/src/lib/grids/grid/grid-validation.service.ts
1
import { Injectable } from '@angular/core';
2
import { FormControl, FormGroup } from '@angular/forms';
3
import { resolveNestedPath } from '../../core/utils';
4
import { ColumnType, GridType, IFieldValidationState, IGridFormGroupCreatedEventArgs, IRecordValidationState, ValidationStatus } from '../common/grid.interface';
5

6
@Injectable()
7
export class IgxGridValidationService {
2✔
8
    /**
9
     * @hidden
10
     * @internal
11
     */
12
    public grid: GridType;
13
    private _validityStates = new Map<any, FormGroup>();
37✔
14
    private _valid = true;
37✔
15

16

17
    /** Gets whether state is valid.
18
    */
19
    public get valid() : boolean {
UNCOV
20
        return this._valid;
×
21
    }
22

23
    /**
24
     * @hidden
25
     * @internal
26
     */
27
    public create(rowId, data) {
UNCOV
28
        let formGroup = this.getFormGroup(rowId);
×
UNCOV
29
        if (!formGroup) {
×
UNCOV
30
            formGroup = new FormGroup({});
×
UNCOV
31
            for (const col of this.grid.columns) {
×
UNCOV
32
                this.addFormControl(formGroup, data, col);
×
33
            }
UNCOV
34
            const args: IGridFormGroupCreatedEventArgs = {
×
35
                formGroup,
36
                owner: this.grid
37
            };
UNCOV
38
            this.grid.formGroupCreated.emit(args);
×
UNCOV
39
            this.add(rowId, formGroup);
×
40
        } else {
41
            // reset to pristine.
UNCOV
42
            for (const col of this.grid.columns) {
×
UNCOV
43
                const formControl = formGroup.get(col.field);
×
UNCOV
44
                if (formControl) {
×
UNCOV
45
                    formControl.markAsPristine();
×
46
                } else {
UNCOV
47
                    this.addFormControl(formGroup, data, col);
×
48
                }
49
            }
50
        }
51

UNCOV
52
        return formGroup;
×
53
    }
54

55
    /**
56
    * @hidden
57
    * @internal
58
    */
59
    private addFormControl(formGroup: FormGroup, data: any, column: ColumnType) {
UNCOV
60
        const value = resolveNestedPath(data || {}, column.field);
×
UNCOV
61
        const field = this.getFieldKey(column.field);
×
UNCOV
62
        const control = new FormControl(value, { updateOn: this.grid.validationTrigger });
×
UNCOV
63
        control.addValidators(column.validators);
×
UNCOV
64
        formGroup.addControl(field, control);
×
UNCOV
65
        control.setValue(value);
×
66
    }
67

68
    /**
69
     * @hidden
70
     * @internal
71
     */
72
    private getFieldKey(path: string) {
UNCOV
73
        const parts = path?.split('.') ?? [];
×
UNCOV
74
        return parts.join('_');
×
75
    }
76

77
    /**
78
     * @hidden
79
     * @internal
80
     */
81
    public getFormGroup(id: any) {
82
        return this._validityStates.get(id);
46,022✔
83
    }
84

85
    /**
86
     * @hidden
87
     * @internal
88
     */
89
    public getFormControl(rowId: any, columnKey: string) {
UNCOV
90
        const formControl = this.getFormGroup(rowId);
×
UNCOV
91
        const field = this.getFieldKey(columnKey);
×
UNCOV
92
        return formControl?.get(field);
×
93
    }
94

95
    /**
96
     * @hidden
97
     * @internal
98
     */
99
    public add(rowId: any, form: FormGroup) {
UNCOV
100
        this._validityStates.set(rowId, form);
×
101
    }
102

103
    /**
104
     * @hidden
105
     * @internal
106
     */
107
    private getValidity(): IRecordValidationState[] {
UNCOV
108
        const states: IRecordValidationState[] = [];
×
UNCOV
109
        this._validityStates.forEach((formGroup, key) => {
×
UNCOV
110
            const state: IFieldValidationState[] = [];
×
UNCOV
111
            for (const col of this.grid.columns) {
×
UNCOV
112
                const colKey = this.getFieldKey(col.field);
×
UNCOV
113
                const control = formGroup.get(colKey);
×
UNCOV
114
                if (control) {
×
UNCOV
115
                    state.push({ field: colKey, status: control.status as ValidationStatus, errors: control.errors })
×
116
                }
117
            }
UNCOV
118
            states.push({ key: key, status: formGroup.status as ValidationStatus, fields: state, errors: formGroup.errors });
×
119
        });
UNCOV
120
        return states;
×
121
    }
122

123
    /**
124
     * Returns all invalid record states.
125
     */
126
    public getInvalid(): IRecordValidationState[] {
UNCOV
127
        const validity = this.getValidity();
×
UNCOV
128
        return validity.filter(x => x.status === 'INVALID');
×
129
    }
130

131
    /**
132
     * @hidden
133
     * @internal
134
     */
135
    public update(rowId: any, rowData: any) {
UNCOV
136
        if (!rowData) return;
×
UNCOV
137
        const keys = Object.keys(rowData);
×
UNCOV
138
        const rowGroup = this.getFormGroup(rowId);
×
UNCOV
139
        for (const key of keys) {
×
UNCOV
140
            const colKey = this.getFieldKey(key);
×
UNCOV
141
            const control = rowGroup?.get(colKey);
×
UNCOV
142
            if (control && control.value !== rowData[key]) {
×
UNCOV
143
                control.setValue(rowData[key], { emitEvent: false });
×
144
            }
145
        }
146

UNCOV
147
        this.updateStatus();
×
148
    }
149

150
    /**
151
     * @hidden
152
     * @internal
153
     * Update validity based on new data.
154
     */
155
    public updateAll(newData: any) {
156
        if (!newData || this._validityStates.size === 0) return;
1✔
UNCOV
157
        for (const rec of newData) {
×
UNCOV
158
            const rowId = rec[this.grid.primaryKey] || rec;
×
UNCOV
159
            if (this.getFormGroup(rowId)) {
×
UNCOV
160
                const recAggregatedData = this.grid.transactions.getAggregatedValue(rowId, true) || rec;
×
UNCOV
161
                this.update(rowId, recAggregatedData);
×
162
            }
163
        }
164
    }
165

166
    /** Marks the associated record or field as touched.
167
     * @param key The id of the record that will be marked as touched.
168
     * @param field Optional. The field from the record that will be marked as touched. If not provided all fields will be touched.
169
    */
170
    public markAsTouched(key: any, field?: string) {
UNCOV
171
        const rowGroup = this.getFormGroup(key);
×
UNCOV
172
        if (!rowGroup) return;
×
UNCOV
173
        rowGroup.markAsTouched();
×
UNCOV
174
        const fields = field ? [field] : this.grid.columns.map(x => x.field);
×
UNCOV
175
        for (const currField of fields) {
×
UNCOV
176
            const colKey = this.getFieldKey(currField);
×
UNCOV
177
            rowGroup?.get(colKey)?.markAsTouched();
×
178
        }
179
    }
180

181
    /**
182
     * @hidden
183
     * @internal
184
     */
185
    private updateStatus() {
UNCOV
186
        const currentValid = this.valid;
×
UNCOV
187
        this._valid = this.getInvalid().length === 0;
×
UNCOV
188
        if (this.valid !== currentValid) {
×
UNCOV
189
            this.grid.validationStatusChange.emit({ status: this.valid ? 'VALID' : 'INVALID', owner: this.grid });
×
190
        }
191
    }
192

193
    /** Clears validation state by key or all states if none is provided.
194
     * @param key Optional. The key of the record for which to clear state.
195
    */
196
    public clear(key?: any) {
UNCOV
197
        if (key !== undefined) {
×
UNCOV
198
            this._validityStates.delete(key);
×
199
        } else {
UNCOV
200
            this._validityStates.clear();
×
201
        }
UNCOV
202
        this.updateStatus();
×
203
    }
204

205
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc