aboutsummaryrefslogtreecommitdiffhomepage
path: root/client/src/app/shared/shared-forms/form-reactive.ts
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/app/shared/shared-forms/form-reactive.ts')
-rw-r--r--client/src/app/shared/shared-forms/form-reactive.ts40
1 files changed, 26 insertions, 14 deletions
diff --git a/client/src/app/shared/shared-forms/form-reactive.ts b/client/src/app/shared/shared-forms/form-reactive.ts
index 30b59c141..07a12c6f6 100644
--- a/client/src/app/shared/shared-forms/form-reactive.ts
+++ b/client/src/app/shared/shared-forms/form-reactive.ts
@@ -1,4 +1,6 @@
1
1import { FormGroup } from '@angular/forms' 2import { FormGroup } from '@angular/forms'
3import { wait } from '@root-helpers/utils'
2import { BuildFormArgument, BuildFormDefaultValues } from '../form-validators/form-validator.model' 4import { BuildFormArgument, BuildFormDefaultValues } from '../form-validators/form-validator.model'
3import { FormValidatorService } from './form-validator.service' 5import { FormValidatorService } from './form-validator.service'
4 6
@@ -22,30 +24,42 @@ export abstract class FormReactive {
22 this.formErrors = formErrors 24 this.formErrors = formErrors
23 this.validationMessages = validationMessages 25 this.validationMessages = validationMessages
24 26
25 this.form.valueChanges.subscribe(() => this.onValueChanged(this.form, this.formErrors, this.validationMessages, false)) 27 this.form.statusChanges.subscribe(async status => {
28 // FIXME: remove when https://github.com/angular/angular/issues/41519 is fixed
29 await this.waitPendingCheck()
30
31 this.onStatusChanged(this.form, this.formErrors, this.validationMessages)
32 })
26 } 33 }
27 34
28 protected forceCheck () { 35 protected async waitPendingCheck () {
29 return this.onValueChanged(this.form, this.formErrors, this.validationMessages, true) 36 if (this.form.status !== 'PENDING') return
37
38 // FIXME: the following line does not work: https://github.com/angular/angular/issues/41519
39 // return firstValueFrom(this.form.statusChanges.pipe(filter(status => status !== 'PENDING')))
40 // So we have to fallback to active wait :/
41
42 do {
43 await wait(10)
44 } while (this.form.status === 'PENDING')
30 } 45 }
31 46
32 protected check () { 47 protected forceCheck () {
33 return this.onValueChanged(this.form, this.formErrors, this.validationMessages, false) 48 this.onStatusChanged(this.form, this.formErrors, this.validationMessages, false)
34 } 49 }
35 50
36 private onValueChanged ( 51 private onStatusChanged (
37 form: FormGroup, 52 form: FormGroup,
38 formErrors: FormReactiveErrors, 53 formErrors: FormReactiveErrors,
39 validationMessages: FormReactiveValidationMessages, 54 validationMessages: FormReactiveValidationMessages,
40 forceCheck = false 55 onlyDirty = true
41 ) { 56 ) {
42 for (const field of Object.keys(formErrors)) { 57 for (const field of Object.keys(formErrors)) {
43 if (formErrors[field] && typeof formErrors[field] === 'object') { 58 if (formErrors[field] && typeof formErrors[field] === 'object') {
44 this.onValueChanged( 59 this.onStatusChanged(
45 form.controls[field] as FormGroup, 60 form.controls[field] as FormGroup,
46 formErrors[field] as FormReactiveErrors, 61 formErrors[field] as FormReactiveErrors,
47 validationMessages[field] as FormReactiveValidationMessages, 62 validationMessages[field] as FormReactiveValidationMessages
48 forceCheck
49 ) 63 )
50 continue 64 continue
51 } 65 }
@@ -56,8 +70,7 @@ export abstract class FormReactive {
56 70
57 if (control.dirty) this.formChanged = true 71 if (control.dirty) this.formChanged = true
58 72
59 if (forceCheck) control.updateValueAndValidity({ emitEvent: false }) 73 if (!control || (onlyDirty && !control.dirty) || !control.enabled || !control.errors) continue
60 if (!control || !control.dirty || !control.enabled || control.valid) continue
61 74
62 const staticMessages = validationMessages[field] 75 const staticMessages = validationMessages[field]
63 for (const key of Object.keys(control.errors)) { 76 for (const key of Object.keys(control.errors)) {
@@ -65,11 +78,10 @@ export abstract class FormReactive {
65 78
66 // Try to find error message in static validation messages first 79 // Try to find error message in static validation messages first
67 // Then check if the validator returns a string that is the error 80 // Then check if the validator returns a string that is the error
68 if (typeof formErrorValue === 'boolean') formErrors[field] += staticMessages[key] + ' ' 81 if (staticMessages[key]) formErrors[field] += staticMessages[key] + ' '
69 else if (typeof formErrorValue === 'string') formErrors[field] += control.errors[key] 82 else if (typeof formErrorValue === 'string') formErrors[field] += control.errors[key]
70 else throw new Error('Form error value of ' + field + ' is invalid') 83 else throw new Error('Form error value of ' + field + ' is invalid')
71 } 84 }
72 } 85 }
73 } 86 }
74
75} 87}