Skip to content

Commit 399cb42

Browse files
committed
fix: handle empty errors object
1 parent f03c2fe commit 399cb42

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
lines changed

projects/xtream/ngx-validation-errors/src/lib/form-validation-container.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export abstract class FormValidationContainer implements AfterContentInit, OnDes
8282
checkErrors() {
8383
const hasError = (!this.formControl.valid && this.formControl.dirty && this.formControl.touched) && !this.validationDisabled;
8484
if (hasError && this.el && this.el.nativeElement) {
85-
this.messages = Object.keys(this.formControl.errors).map(error => {
85+
this.messages = Object.keys(this.formControl.errors || {}).map(error => {
8686
const fieldName = this.formControlName;
8787
const errorKey = `${toScreamingSnakeCase(fieldName + '')}.ERRORS.${toScreamingSnakeCase(error)}`;
8888
if (this.messageProvider &&
@@ -92,7 +92,7 @@ export abstract class FormValidationContainer implements AfterContentInit, OnDes
9292
return `${this.validationContext}.${errorKey}`;
9393
}
9494
});
95-
const params = Object.values(this.formControl.errors).reduce((a, b) => {
95+
const params = Object.values(this.formControl.errors || {}).reduce((a, b) => {
9696
a = {...a, ...b};
9797
return a;
9898
}, {});

src/app/lazy/lazy-form/lazy-form.component.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {Component, OnInit, ViewChild} from '@angular/core';
22
import {AbstractControl, FormArray, FormControl, FormGroup, Validators} from '@angular/forms';
33
import {TranslateService} from '@ngx-translate/core';
44
import {ValidationContextComponent} from '@xtream/ngx-validation-errors';
5+
import {distinctUntilChanged, tap} from 'rxjs/operators';
56

67
function minCheckSelected(size: number) {
78
return (control: AbstractControl) => {
@@ -21,7 +22,10 @@ function minCheckSelected(size: number) {
2122
})
2223
export class LazyFormComponent implements OnInit {
2324

24-
@ViewChild('firstForm', {read: ValidationContextComponent, static: true}) validationContext: ValidationContextComponent;
25+
@ViewChild('firstForm', {
26+
read: ValidationContextComponent,
27+
static: true
28+
}) validationContext: ValidationContextComponent;
2529

2630
heroForm: FormGroup;
2731

@@ -35,9 +39,21 @@ export class LazyFormComponent implements OnInit {
3539
constructor(private translateService: TranslateService) {
3640
this.heroForm = new FormGroup({
3741
name: new FormControl(null, [Validators.required, Validators.minLength(4)]),
38-
surname: new FormControl(null, [Validators.required, Validators.maxLength(1000)]),
42+
surname: new FormControl({value: null, disabled: true}, [Validators.required, Validators.maxLength(1000)]),
3943
checkBoxes: new FormArray(this.boxesInfo.map(a => new FormControl()), [minCheckSelected(1)])
4044
});
45+
46+
this.heroForm.valueChanges.pipe(
47+
distinctUntilChanged(),
48+
tap(v => {
49+
console.debug('v', v);
50+
if (v.name) {
51+
this.heroForm.controls['surname'].enable({emitEvent: false});
52+
} else {
53+
this.heroForm.controls['surname'].disable({emitEvent: false});
54+
}
55+
})
56+
).subscribe()
4157
}
4258

4359
ngOnInit(): void {

0 commit comments

Comments
 (0)