]>
Commit | Line | Data |
---|---|---|
df98563e | 1 | import { FormGroup } from '@angular/forms' |
4b2f33f3 C |
2 | |
3 | export abstract class FormReactive { | |
df98563e C |
4 | abstract form: FormGroup |
5 | abstract formErrors: Object | |
6 | abstract validationMessages: Object | |
4b2f33f3 | 7 | |
df98563e | 8 | abstract buildForm (): void |
4b2f33f3 | 9 | |
df98563e | 10 | protected onValueChanged (data?: any) { |
4b2f33f3 C |
11 | for (const field in this.formErrors) { |
12 | // clear previous error message (if any) | |
df98563e C |
13 | this.formErrors[field] = '' |
14 | const control = this.form.get(field) | |
4b2f33f3 C |
15 | |
16 | if (control && control.dirty && !control.valid) { | |
df98563e | 17 | const messages = this.validationMessages[field] |
4b2f33f3 | 18 | for (const key in control.errors) { |
df98563e | 19 | this.formErrors[field] += messages[key] + ' ' |
4b2f33f3 C |
20 | } |
21 | } | |
22 | } | |
23 | } | |
bf57d5ee C |
24 | |
25 | // Same as onValueChanged but force checking even if the field is not dirty | |
df98563e | 26 | protected forceCheck () { |
bf57d5ee C |
27 | for (const field in this.formErrors) { |
28 | // clear previous error message (if any) | |
df98563e C |
29 | this.formErrors[field] = '' |
30 | const control = this.form.get(field) | |
bf57d5ee C |
31 | |
32 | if (control && !control.valid) { | |
df98563e | 33 | const messages = this.validationMessages[field] |
bf57d5ee | 34 | for (const key in control.errors) { |
df98563e | 35 | this.formErrors[field] += messages[key] + ' ' |
bf57d5ee C |
36 | } |
37 | } | |
38 | } | |
39 | } | |
4b2f33f3 | 40 | } |