diff options
Diffstat (limited to 'client/src/app/shared/shared-forms')
-rw-r--r-- | client/src/app/shared/shared-forms/form-reactive.service.ts | 2 | ||||
-rw-r--r-- | client/src/app/shared/shared-forms/form-reactive.ts | 82 |
2 files changed, 9 insertions, 75 deletions
diff --git a/client/src/app/shared/shared-forms/form-reactive.service.ts b/client/src/app/shared/shared-forms/form-reactive.service.ts index 69077eb07..f1b7e0ef2 100644 --- a/client/src/app/shared/shared-forms/form-reactive.service.ts +++ b/client/src/app/shared/shared-forms/form-reactive.service.ts | |||
@@ -56,7 +56,7 @@ export class FormReactiveService { | |||
56 | } | 56 | } |
57 | } | 57 | } |
58 | 58 | ||
59 | protected forceCheck (form: FormGroup, formErrors: any, validationMessages: FormReactiveValidationMessages) { | 59 | forceCheck (form: FormGroup, formErrors: any, validationMessages: FormReactiveValidationMessages) { |
60 | this.onStatusChanged({ form, formErrors, validationMessages, onlyDirty: false }) | 60 | this.onStatusChanged({ form, formErrors, validationMessages, onlyDirty: false }) |
61 | } | 61 | } |
62 | 62 | ||
diff --git a/client/src/app/shared/shared-forms/form-reactive.ts b/client/src/app/shared/shared-forms/form-reactive.ts index acaeaba33..d1e7be802 100644 --- a/client/src/app/shared/shared-forms/form-reactive.ts +++ b/client/src/app/shared/shared-forms/form-reactive.ts | |||
@@ -1,11 +1,9 @@ | |||
1 | import { AbstractControl, FormGroup } from '@angular/forms' | 1 | import { FormGroup } from '@angular/forms' |
2 | import { wait } from '@root-helpers/utils' | ||
3 | import { BuildFormArgument, BuildFormDefaultValues } from '../form-validators/form-validator.model' | 2 | import { BuildFormArgument, BuildFormDefaultValues } from '../form-validators/form-validator.model' |
4 | import { FormReactiveErrors, FormReactiveValidationMessages } from './form-reactive.service' | 3 | import { FormReactiveService, FormReactiveValidationMessages } from './form-reactive.service' |
5 | import { FormValidatorService } from './form-validator.service' | ||
6 | 4 | ||
7 | export abstract class FormReactive { | 5 | export abstract class FormReactive { |
8 | protected abstract formValidatorService: FormValidatorService | 6 | protected abstract formReactiveService: FormReactiveService |
9 | protected formChanged = false | 7 | protected formChanged = false |
10 | 8 | ||
11 | form: FormGroup | 9 | form: FormGroup |
@@ -13,86 +11,22 @@ export abstract class FormReactive { | |||
13 | validationMessages: FormReactiveValidationMessages | 11 | validationMessages: FormReactiveValidationMessages |
14 | 12 | ||
15 | buildForm (obj: BuildFormArgument, defaultValues: BuildFormDefaultValues = {}) { | 13 | buildForm (obj: BuildFormArgument, defaultValues: BuildFormDefaultValues = {}) { |
16 | const { formErrors, validationMessages, form } = this.formValidatorService.buildForm(obj, defaultValues) | 14 | const { formErrors, validationMessages, form } = this.formReactiveService.buildForm(obj, defaultValues) |
17 | 15 | ||
18 | this.form = form | 16 | this.form = form |
19 | this.formErrors = formErrors | 17 | this.formErrors = formErrors |
20 | this.validationMessages = validationMessages | 18 | this.validationMessages = validationMessages |
21 | |||
22 | this.form.statusChanges.subscribe(async () => { | ||
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 | }) | ||
28 | } | 19 | } |
29 | 20 | ||
30 | protected async waitPendingCheck () { | 21 | protected async waitPendingCheck () { |
31 | if (this.form.status !== 'PENDING') return | 22 | return this.formReactiveService.waitPendingCheck(this.form) |
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') | ||
40 | } | 23 | } |
41 | 24 | ||
42 | protected markAllAsDirty (controlsArg?: { [ key: string ]: AbstractControl }) { | 25 | protected markAllAsDirty () { |
43 | const controls = controlsArg || this.form.controls | 26 | return this.formReactiveService.markAllAsDirty(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 | } | 27 | } |
56 | 28 | ||
57 | protected forceCheck () { | 29 | protected forceCheck () { |
58 | this.onStatusChanged(this.form, this.formErrors, this.validationMessages, false) | 30 | return this.formReactiveService.forceCheck(this.form, this.formErrors, this.validationMessages) |
59 | } | ||
60 | |||
61 | private onStatusChanged ( | ||
62 | form: FormGroup, | ||
63 | formErrors: FormReactiveErrors, | ||
64 | validationMessages: FormReactiveValidationMessages, | ||
65 | onlyDirty = true | ||
66 | ) { | ||
67 | for (const field of Object.keys(formErrors)) { | ||
68 | if (formErrors[field] && typeof formErrors[field] === 'object') { | ||
69 | this.onStatusChanged( | ||
70 | form.controls[field] as FormGroup, | ||
71 | formErrors[field] as FormReactiveErrors, | ||
72 | validationMessages[field] as FormReactiveValidationMessages, | ||
73 | onlyDirty | ||
74 | ) | ||
75 | continue | ||
76 | } | ||
77 | |||
78 | // clear previous error message (if any) | ||
79 | formErrors[field] = '' | ||
80 | const control = form.get(field) | ||
81 | |||
82 | if (control.dirty) this.formChanged = true | ||
83 | |||
84 | if (!control || (onlyDirty && !control.dirty) || !control.enabled || !control.errors) continue | ||
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 | ||
92 | if (staticMessages[key]) formErrors[field] += staticMessages[key] + ' ' | ||
93 | else if (typeof formErrorValue === 'string') formErrors[field] += control.errors[key] | ||
94 | else throw new Error('Form error value of ' + field + ' is invalid') | ||
95 | } | ||
96 | } | ||
97 | } | 31 | } |
98 | } | 32 | } |