1 import { FormGroup } from '@angular/forms'
2 import { BuildFormArgument, BuildFormDefaultValues, FormValidatorService } from '@app/shared/forms/form-validators/form-validator.service'
4 export type FormReactiveErrors = { [ id: string ]: string }
5 export type FormReactiveValidationMessages = {
7 [ name: string ]: string
11 export abstract class FormReactive {
12 protected abstract formValidatorService: FormValidatorService
15 formErrors: FormReactiveErrors
16 validationMessages: FormReactiveValidationMessages
18 buildForm (obj: BuildFormArgument, defaultValues: BuildFormDefaultValues = {}) {
19 const { formErrors, validationMessages, form } = this.formValidatorService.buildForm(obj, defaultValues)
22 this.formErrors = formErrors
23 this.validationMessages = validationMessages
25 this.form.valueChanges.subscribe(data => this.onValueChanged(false))
28 protected onValueChanged (forceCheck = false) {
29 for (const field in this.formErrors) {
30 // clear previous error message (if any)
31 this.formErrors[ field ] = ''
32 const control = this.form.get(field)
34 // Don't care if dirty on force check
35 const isDirty = control.dirty || forceCheck === true
36 if (control && isDirty && !control.valid) {
37 const messages = this.validationMessages[ field ]
38 for (const key in control.errors) {
39 this.formErrors[ field ] += messages[ key ] + ' '
45 protected forceCheck () {
46 return this.onValueChanged(true)