diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts index 7ef578e7aa5..5b14e04be60 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.spec.ts @@ -15,6 +15,7 @@ import { HIDDEN_MATCHER_PROVIDER, REQUIRED_MATCHER_PROVIDER, } from '@ng-dynamic-forms/core'; +import { Subject } from 'rxjs'; import { getMockFormBuilderService } from '../../testing/form-builder-service.mock'; import { @@ -99,6 +100,29 @@ describe('DSDynamicTypeBindRelationService test suite', () => { expect(subscriptions).toHaveSize(1); }); + describe('Test control reset on MATCH_VISIBLE', () => { + it('should reset control when MATCH_VISIBLE becomes false', () => { + const testModel = mockInputWithTypeBindModel; + testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type'); + + const control = new UntypedFormControl(); + control.setValue('test value'); + + const typeModel: any = (service as any).formBuilderService.getTypeBindModel(); + + typeModel.valueChanges = new Subject(); + + typeModel.value = 'boundType'; + + service.subscribeRelations(testModel, control); + + typeModel.value = 'anotherType'; + typeModel.valueChanges.next('anotherType'); + + expect(control.value).toBeNull(); + }); + }); + it('Expect hasMatch to be true (ie. this should be hidden)', () => { const testModel = mockInputWithTypeBindModel; testModel.typeBindRelations = getTypeBindRelations(['boundType'], 'dc.type'); diff --git a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts index 68a77c8f9ad..99d07d1518a 100644 --- a/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts +++ b/src/app/shared/form/builder/ds-dynamic-form-ui/ds-dynamic-type-bind-relation.service.ts @@ -19,6 +19,7 @@ import { DynamicFormControlModel, DynamicFormControlRelation, DynamicFormRelationService, + MATCH_VISIBLE, OR_OPERATOR, } from '@ng-dynamic-forms/core'; import { Subscription } from 'rxjs'; @@ -205,6 +206,10 @@ export class DsDynamicTypeBindRelationService { if (relation !== undefined) { const hasMatch = this.matchesCondition(relation, matcher); matcher.onChange(hasMatch, model, control, this.injector); + // When field becomes hidden, reset its FormControl to clear stale values + if (relation.match === MATCH_VISIBLE && !hasMatch && hasValue(control)) { + control.reset(); + } } }); }