Skip to content
Snippets Groups Projects
Commit bd724f13 authored by Sebastian Bergandy's avatar Sebastian Bergandy :keyboard:
Browse files

Merge branch 'fix-validation-change-detection' into 'main'

Fix change detection on validation

See merge request !66
parents f9d2d5b5 2281cc0e
No related branches found
No related tags found
1 merge request!66Fix change detection on validation
...@@ -56,7 +56,7 @@ describe('CheckboxEditorComponent', () => { ...@@ -56,7 +56,7 @@ describe('CheckboxEditorComponent', () => {
describe('hasError', () => { describe('hasError', () => {
it('should return true', () => { it('should return true', () => {
component.fieldControl.setErrors({ 1: createInvalidParam() }); component.fieldControl.setErrors({ 1: createInvalidParam() });
component._updateInvalidParams();
const result: boolean = component.hasError; const result: boolean = component.hasError;
expect(result).toBe(true); expect(result).toBe(true);
...@@ -76,6 +76,7 @@ describe('CheckboxEditorComponent', () => { ...@@ -76,6 +76,7 @@ describe('CheckboxEditorComponent', () => {
describe('error message', () => { describe('error message', () => {
it('should show', () => { it('should show', () => {
component.fieldControl.setErrors({ 1: createInvalidParam() }); component.fieldControl.setErrors({ 1: createInvalidParam() });
component._updateInvalidParams();
fixture.detectChanges(); fixture.detectChanges();
const element: HTMLElement = getElementFromFixture(fixture, errorMessageId); const element: HTMLElement = getElementFromFixture(fixture, errorMessageId);
......
...@@ -33,6 +33,7 @@ export abstract class FormControlEditorAbstractComponent implements ControlValue ...@@ -33,6 +33,7 @@ export abstract class FormControlEditorAbstractComponent implements ControlValue
readonly fieldControl: UntypedFormControl = new UntypedFormControl(); readonly fieldControl: UntypedFormControl = new UntypedFormControl();
public onChange = (text: string | Date) => undefined; public onChange = (text: string | Date) => undefined;
public onTouched = () => undefined; public onTouched = () => undefined;
public invalidParams: InvalidParam[] = [];
private changesSubscr: Subscription; private changesSubscr: Subscription;
private statusSubscr: Subscription; private statusSubscr: Subscription;
...@@ -81,12 +82,16 @@ export abstract class FormControlEditorAbstractComponent implements ControlValue ...@@ -81,12 +82,16 @@ export abstract class FormControlEditorAbstractComponent implements ControlValue
setErrors(): void { setErrors(): void {
if (this.control) { if (this.control) {
this.fieldControl.setErrors(this.control.errors); this.fieldControl.setErrors(this.control.errors);
if (this.control.invalid) this.fieldControl.markAsTouched(); if (this.control.invalid) {
this._updateInvalidParams();
this.fieldControl.markAsTouched();
}
} }
} }
get invalidParams(): InvalidParam[] { _updateInvalidParams(): void {
return this.fieldControl.errors ? this.invalidParams =
this.fieldControl.errors ?
Object.keys(this.fieldControl.errors).map((key) => <InvalidParam>this.fieldControl.errors[key]) Object.keys(this.fieldControl.errors).map((key) => <InvalidParam>this.fieldControl.errors[key])
: []; : [];
} }
......
...@@ -23,6 +23,6 @@ Die sprachspezifischen Genehmigungen und Beschränkungen ...@@ -23,6 +23,6 @@ Die sprachspezifischen Genehmigungen und Beschränkungen
unter der Lizenz sind dem Lizenztext zu entnehmen. unter der Lizenz sind dem Lizenztext zu entnehmen.
--> -->
@for (invalidParam of invalidParams; track invalidParam) { @for (validationMessage of validationMessages; track validationMessage) {
<span>{{ message(invalidParam) }}</span> <span>{{ validationMessage }}</span>
} }
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
* Die sprachspezifischen Genehmigungen und Beschränkungen * Die sprachspezifischen Genehmigungen und Beschränkungen
* unter der Lizenz sind dem Lizenztext zu entnehmen. * unter der Lizenz sind dem Lizenztext zu entnehmen.
*/ */
import { InvalidParam, getMessageForInvalidParam } from '@alfa-client/tech-shared'; import { getMessageForInvalidParam, InvalidParam } from '@alfa-client/tech-shared';
import { Component, Input } from '@angular/core'; import { Component, Input } from '@angular/core';
@Component({ @Component({
...@@ -32,9 +32,10 @@ import { Component, Input } from '@angular/core'; ...@@ -32,9 +32,10 @@ import { Component, Input } from '@angular/core';
}) })
export class ValidationErrorComponent { export class ValidationErrorComponent {
@Input() label: string; @Input() label: string;
@Input() invalidParams: InvalidParam[];
public message(invalidParam: InvalidParam): string { @Input() set invalidParams(value: InvalidParam[]) {
return getMessageForInvalidParam(this.label, invalidParam); this.validationMessages = value.map((invalidParam: InvalidParam) => getMessageForInvalidParam(this.label, invalidParam));
} }
public validationMessages: string[] = [];
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment