import { Components, Displays } from '@formio/js'; type ValidationResult = unknown; interface ComponentInstance { __gdsValidationAttempted?: boolean; component?: { clearOnHide?: boolean; }; pristine: boolean; root?: ValidationRoot; setPristine?: (pristine: boolean) => void; setCustomValidity: (messages: unknown) => ValidationResult; visible?: boolean; } interface ValidationRoot { __gdsValidationAttemptInProgress?: boolean; submitting?: boolean; } interface ComponentPrototype { __gdsDeferPristineErrorsInstalled?: boolean; checkComponentConditions: ( this: ComponentInstance, data?: unknown, flags?: unknown, row?: unknown, ) => boolean; setComponentValidity: ( this: ComponentInstance, errors: unknown[], dirty: boolean, silentCheck: boolean, ) => ValidationResult; } interface ComponentConstructor { prototype: ComponentPrototype; } interface WizardPrototype extends ValidationRoot { __gdsDeferPristineErrorsInstalled?: boolean; nextPage: (...args: unknown[]) => unknown; } interface WizardConstructor { prototype: WizardPrototype; } /** * Restores Form.io v4 validation timing in v5. Errors on pristine components are * not rendered (`setCustomValidity` is deferred) until the component has changed * or is validated as dirty. Validation itself still runs. */ export const installDeferredPristineErrors = ( ComponentClass: ComponentConstructor = Components.components.component, WizardClass: WizardConstructor = Displays.getDisplay('wizard'), ): void => { const prototype = ComponentClass.prototype; if (!prototype.__gdsDeferPristineErrorsInstalled) { prototype.__gdsDeferPristineErrorsInstalled = true; const originalSetComponentValidity = prototype.setComponentValidity; const originalCheckComponentConditions = prototype.checkComponentConditions; prototype.checkComponentConditions = function ( this: ComponentInstance, data?: unknown, flags?: unknown, row?: unknown, ): boolean { const wasVisible = this.visible !== false; const result = originalCheckComponentConditions.call(this, data, flags, row); if ( wasVisible && this.visible === false && this.component?.clearOnHide !== false ) { delete this.__gdsValidationAttempted; if (this.setPristine) { this.setPristine(true); } else { this.pristine = true; } this.setCustomValidity(''); } return result; }; prototype.setComponentValidity = function ( this: ComponentInstance, errors: unknown[], dirty: boolean, silentCheck: boolean, ): ValidationResult { const validationAttemptInProgress = Boolean( this.root?.submitting || this.root?.__gdsValidationAttemptInProgress, ); if (dirty && validationAttemptInProgress && this.visible !== false) { this.__gdsValidationAttempted = true; } if (this.pristine && !this.__gdsValidationAttempted) { return silentCheck ? [] : this.setCustomValidity(''); } return originalSetComponentValidity.call(this, errors, dirty, silentCheck); }; } const wizardPrototype = WizardClass.prototype; if (!wizardPrototype.__gdsDeferPristineErrorsInstalled) { wizardPrototype.__gdsDeferPristineErrorsInstalled = true; const originalNextPage = wizardPrototype.nextPage; wizardPrototype.nextPage = function ( this: WizardPrototype, ...args: unknown[] ): unknown { const previousState = this.__gdsValidationAttemptInProgress; this.__gdsValidationAttemptInProgress = true; try { return originalNextPage.apply(this, args); } finally { this.__gdsValidationAttemptInProgress = previousState; } }; } };