]>
Commit | Line | Data |
---|---|---|
8b69f9f0 | 1 | import { AbstractControl, FormGroup } from '@angular/forms' |
cc4bf76c | 2 | import { wait } from '@root-helpers/utils' |
7ed1edbb | 3 | import { BuildFormArgument, BuildFormDefaultValues } from '../form-validators/form-validator.model' |
d12b40fb | 4 | import { FormReactiveErrors, FormReactiveValidationMessages } from './form-reactive.service' |
7ed1edbb | 5 | import { FormValidatorService } from './form-validator.service' |
d18d6478 | 6 | |
4b2f33f3 | 7 | export abstract class FormReactive { |
d18d6478 | 8 | protected abstract formValidatorService: FormValidatorService |
772d5642 | 9 | protected formChanged = false |
4b2f33f3 | 10 | |
d18d6478 | 11 | form: FormGroup |
26a008fe | 12 | formErrors: any // To avoid casting in template because of string | FormReactiveErrors |
d18d6478 | 13 | validationMessages: FormReactiveValidationMessages |
4b2f33f3 | 14 | |
d18d6478 C |
15 | buildForm (obj: BuildFormArgument, defaultValues: BuildFormDefaultValues = {}) { |
16 | const { formErrors, validationMessages, form } = this.formValidatorService.buildForm(obj, defaultValues) | |
4b2f33f3 | 17 | |
d18d6478 C |
18 | this.form = form |
19 | this.formErrors = formErrors | |
20 | this.validationMessages = validationMessages | |
21 | ||
c729caf6 | 22 | this.form.statusChanges.subscribe(async () => { |
cc4bf76c C |
23 | // FIXME: remove when https://github.com/angular/angular/issues/41519 is fixed |
24 | await this.waitPendingCheck() | |
25 | ||
26 | this.onStatusChanged(this.form, this.formErrors, this.validationMessages) | |
27 | }) | |
3866f1a0 C |
28 | } |
29 | ||
cc4bf76c C |
30 | protected async waitPendingCheck () { |
31 | if (this.form.status !== 'PENDING') return | |
32 | ||
33 | // FIXME: the following line does not work: https://github.com/angular/angular/issues/41519 | |
34 | // return firstValueFrom(this.form.statusChanges.pipe(filter(status => status !== 'PENDING'))) | |
35 | // So we have to fallback to active wait :/ | |
36 | ||
37 | do { | |
38 | await wait(10) | |
39 | } while (this.form.status === 'PENDING') | |
3866f1a0 C |
40 | } |
41 | ||
8b69f9f0 C |
42 | protected markAllAsDirty (controlsArg?: { [ key: string ]: AbstractControl }) { |
43 | const controls = controlsArg || this.form.controls | |
44 | ||
45 | for (const key of Object.keys(controls)) { | |
46 | const control = controls[key] | |
47 | ||
48 | if (control instanceof FormGroup) { | |
49 | this.markAllAsDirty(control.controls) | |
50 | continue | |
51 | } | |
52 | ||
53 | control.markAsDirty() | |
54 | } | |
55 | } | |
56 | ||
cc4bf76c C |
57 | protected forceCheck () { |
58 | this.onStatusChanged(this.form, this.formErrors, this.validationMessages, false) | |
4b2f33f3 | 59 | } |
bf57d5ee | 60 | |
cc4bf76c | 61 | private onStatusChanged ( |
3866f1a0 C |
62 | form: FormGroup, |
63 | formErrors: FormReactiveErrors, | |
64 | validationMessages: FormReactiveValidationMessages, | |
cc4bf76c | 65 | onlyDirty = true |
3866f1a0 C |
66 | ) { |
67 | for (const field of Object.keys(formErrors)) { | |
68 | if (formErrors[field] && typeof formErrors[field] === 'object') { | |
cc4bf76c | 69 | this.onStatusChanged( |
3866f1a0 C |
70 | form.controls[field] as FormGroup, |
71 | formErrors[field] as FormReactiveErrors, | |
8b69f9f0 C |
72 | validationMessages[field] as FormReactiveValidationMessages, |
73 | onlyDirty | |
3866f1a0 C |
74 | ) |
75 | continue | |
76 | } | |
77 | ||
bf57d5ee | 78 | // clear previous error message (if any) |
9df52d66 | 79 | formErrors[field] = '' |
3866f1a0 | 80 | const control = form.get(field) |
bf57d5ee | 81 | |
772d5642 C |
82 | if (control.dirty) this.formChanged = true |
83 | ||
cc4bf76c | 84 | if (!control || (onlyDirty && !control.dirty) || !control.enabled || !control.errors) continue |
3c065fe3 C |
85 | |
86 | const staticMessages = validationMessages[field] | |
87 | for (const key of Object.keys(control.errors)) { | |
88 | const formErrorValue = control.errors[key] | |
89 | ||
90 | // Try to find error message in static validation messages first | |
91 | // Then check if the validator returns a string that is the error | |
cc4bf76c | 92 | if (staticMessages[key]) formErrors[field] += staticMessages[key] + ' ' |
3c065fe3 C |
93 | else if (typeof formErrorValue === 'string') formErrors[field] += control.errors[key] |
94 | else throw new Error('Form error value of ' + field + ' is invalid') | |
bf57d5ee C |
95 | } |
96 | } | |
97 | } | |
4b2f33f3 | 98 | } |