1 import { AbstractControl, FormGroup } from '@angular/forms'
2 import { wait } from '@root-helpers/utils'
3 import { BuildFormArgument, BuildFormDefaultValues } from '../form-validators/form-validator.model'
4 import { FormReactiveErrors, FormReactiveValidationMessages } from './form-reactive.service'
5 import { FormValidatorService } from './form-validator.service'
7 export abstract class FormReactive {
8 protected abstract formValidatorService: FormValidatorService
9 protected formChanged = false
12 formErrors: any // To avoid casting in template because of string | FormReactiveErrors
13 validationMessages: FormReactiveValidationMessages
15 buildForm (obj: BuildFormArgument, defaultValues: BuildFormDefaultValues = {}) {
16 const { formErrors, validationMessages, form } = this.formValidatorService.buildForm(obj, defaultValues)
19 this.formErrors = formErrors
20 this.validationMessages = validationMessages
22 this.form.statusChanges.subscribe(async () => {
23 // FIXME: remove when https://github.com/angular/angular/issues/41519 is fixed
24 await this.waitPendingCheck()
26 this.onStatusChanged(this.form, this.formErrors, this.validationMessages)
30 protected async waitPendingCheck () {
31 if (this.form.status !== 'PENDING') return
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 :/
39 } while (this.form.status === 'PENDING')
42 protected markAllAsDirty (controlsArg?: { [ key: string ]: AbstractControl }) {
43 const controls = controlsArg || this.form.controls
45 for (const key of Object.keys(controls)) {
46 const control = controls[key]
48 if (control instanceof FormGroup) {
49 this.markAllAsDirty(control.controls)
57 protected forceCheck () {
58 this.onStatusChanged(this.form, this.formErrors, this.validationMessages, false)
61 private onStatusChanged (
63 formErrors: FormReactiveErrors,
64 validationMessages: FormReactiveValidationMessages,
67 for (const field of Object.keys(formErrors)) {
68 if (formErrors[field] && typeof formErrors[field] === 'object') {
70 form.controls[field] as FormGroup,
71 formErrors[field] as FormReactiveErrors,
72 validationMessages[field] as FormReactiveValidationMessages,
78 // clear previous error message (if any)
79 formErrors[field] = ''
80 const control = form.get(field)
82 if (control.dirty) this.formChanged = true
84 if (!control || (onlyDirty && !control.dirty) || !control.enabled || !control.errors) continue
86 const staticMessages = validationMessages[field]
87 for (const key of Object.keys(control.errors)) {
88 const formErrorValue = control.errors[key]
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')