diff --git a/alfa-client/libs/design-component/src/lib/form/formcontrol-editor.abstract.component.ts b/alfa-client/libs/design-component/src/lib/form/formcontrol-editor.abstract.component.ts index d84fcd376b6360c7f3670e9bb2c5340504d8fd29..29b75f0e5b5eabbeec9a0fe79822ffef7fc1d9c7 100644 --- a/alfa-client/libs/design-component/src/lib/form/formcontrol-editor.abstract.component.ts +++ b/alfa-client/libs/design-component/src/lib/form/formcontrol-editor.abstract.component.ts @@ -42,12 +42,16 @@ export abstract class FormControlEditorAbstractComponent implements ControlValue constructor(@Self() @Optional() public control: NgControl | null) { if (this.control) this.control.valueAccessor = this; + this._changesSubscr = this.fieldControl.valueChanges.subscribe((value: unknown) => { + this._fieldControlOnChangeHandler(value); + }); } ngOnInit(): void { - this._changesSubscr = this.fieldControl.valueChanges.subscribe(this._fieldControlOnChangeHandler); if (this.control) { - this._statusSubscr = this.control.statusChanges.subscribe(this.setErrors); + this._statusSubscr = this.control.statusChanges.subscribe(() => { + this.setErrors(); + }); } } @@ -82,24 +86,27 @@ export abstract class FormControlEditorAbstractComponent implements ControlValue } setErrors(): void { - if (this.control) { - this.fieldControl.setErrors(this.control.errors); - this._updateInvalidParams(); - } + if (!this.control) return; + + this.fieldControl.setErrors(this.control.errors); + this._updateInvalidParams(); } removeErrors(): void { + if (!this.control) return; + this.fieldControl.setErrors(null); this._updateInvalidParams(); this._clearAllParentErrors(this.control.control); } _clearAllParentErrors(control: AbstractControl): void { - const parent: AbstractControl = control.parent; - if (parent) { - parent.setErrors(null); - this._clearAllParentErrors(parent); - } + const parent: AbstractControl = control?.parent; + + if (!parent) return; + + parent.setErrors(null); + this._clearAllParentErrors(parent); } _updateInvalidParams(): void {