From 6a38dda66a7d3cda7bbd9d521836f84688d38421 Mon Sep 17 00:00:00 2001 From: Siva Rama Krishna Date: Thu, 29 Oct 2026 18:34:59 +0530 Subject: [PATCH] fix(field): keep server validation message on the form control displayValidationMessage() used setErrors() to put the server-side validatemessage on the FormControl. setErrors() is a one-off override, so any later updateValueAndValidity() call wiped it out. That happens when the base or subclass setValue() runs after updateComponentCommonProperties(), and when the [formControl] or [required] directives set up or change. It used to work only because setErrors() was wrapped in setTimeout(100). Add a validator to fieldControl that returns { message: true } while angularPConnectData.validateMessage is set, then re-run validation. The error is now re-applied every time validation runs and clears on its own when the message goes away. shows reliably without the timeout. The validator is attached in displayValidationMessage() because some subclasses create their own fieldControl. --- .../src/lib/_components/field/field.base.ts | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/packages/angular-sdk-components/src/lib/_components/field/field.base.ts b/packages/angular-sdk-components/src/lib/_components/field/field.base.ts index f10feb76..5c19192e 100644 --- a/packages/angular-sdk-components/src/lib/_components/field/field.base.ts +++ b/packages/angular-sdk-components/src/lib/_components/field/field.base.ts @@ -1,5 +1,5 @@ import { Directive, inject, Input, OnDestroy, OnInit, ChangeDetectorRef } from '@angular/core'; -import { FormControl, FormGroup } from '@angular/forms'; +import { FormControl, FormGroup, ValidatorFn } from '@angular/forms'; import { AngularPConnectData, AngularPConnectService } from '../../_bridge/angular-pconnect'; import { Utils } from '../../_helpers/utils'; @@ -144,14 +144,20 @@ export class FieldBase implements OnInit, OnDestroy { * Displays the validation message if it exists. */ private displayValidationMessage(): void { + // A validator (rather than a one-off setErrors) is used so the error survives later + // updateValueAndValidity() calls, e.g. from setValue() or [formControl]/[required] directive setup. + if (!this.fieldControl.hasValidator(this.validateMessageValidator)) { + this.fieldControl.addValidators(this.validateMessageValidator); + } + this.fieldControl.updateValueAndValidity({ emitEvent: false }); + if (this.angularPConnectData.validateMessage) { - setTimeout(() => { - this.fieldControl.setErrors({ message: true }); - this.fieldControl.markAsTouched(); - }, 100); + this.fieldControl.markAsTouched(); } } + private readonly validateMessageValidator: ValidatorFn = () => (this.angularPConnectData?.validateMessage ? { message: true } : null); + private updateHelperText(): void { this.helperText = this.bFieldMessageVisible$ ? this.fieldMessage : this.defaultHelperText; }