diff --git a/src/app/core/common-components/entity-form/entity-form.service.spec.ts b/src/app/core/common-components/entity-form/entity-form.service.spec.ts index 777745b9b0..2bfa0fdacc 100644 --- a/src/app/core/common-components/entity-form/entity-form.service.spec.ts +++ b/src/app/core/common-components/entity-form/entity-form.service.spec.ts @@ -145,6 +145,36 @@ describe("EntityFormService", () => { expect(unsavedChanges.pending).toBeFalse(); }); + it("should reset form on cancel, including special fields with getter", async () => { + class MockEntity extends Entity { + @DatabaseField() simpleField = "original"; + + @DatabaseField() get getterField(): string { + return this._getterValue; + } + set getterField(value) { + this._getterValue = value; + } + private _getterValue: string = "original value"; + + @DatabaseField() emptyField; + } + + const formFields = ["simpleField", "getterField", "emptyField"]; + const mockEntity = new MockEntity(); + const formGroup = service.createFormGroup(formFields, mockEntity); + + formGroup.get("simpleField").setValue("new"); + formGroup.get("getterField").setValue("new value"); + formGroup.get("emptyField").setValue("value"); + + service.resetForm(formGroup, mockEntity); + + expect(formGroup.get("simpleField").value).toBe("original"); + expect(formGroup.get("getterField").value).toBe("original value"); + expect(formGroup.get("emptyField").value).toBeUndefined(); + }); + it("should reset state once navigation happens", async () => { const router = TestBed.inject(Router); router.resetConfig([{ path: "test", component: NotFoundComponent }]); diff --git a/src/app/core/common-components/entity-form/entity-form.service.ts b/src/app/core/common-components/entity-form/entity-form.service.ts index 2cf4092961..a78ba87f6c 100644 --- a/src/app/core/common-components/entity-form/entity-form.service.ts +++ b/src/app/core/common-components/entity-form/entity-form.service.ts @@ -7,7 +7,6 @@ import { EntitySchemaService } from "../../entity/schema/entity-schema.service"; import { DynamicValidatorsService } from "./dynamic-form-validators/dynamic-validators.service"; import { EntityAbility } from "../../permissions/ability/entity-ability"; import { InvalidFormFieldError } from "./invalid-form-field.error"; -import { omit } from "lodash-es"; import { UnsavedChangesService } from "../../entity-details/form/unsaved-changes.service"; import { ActivationStart, Router } from "@angular/router"; import { Subscription } from "rxjs"; @@ -245,11 +244,10 @@ export class EntityFormService { } resetForm(form: EntityForm, entity: E) { - // Patch form with values from the entity - form.patchValue(entity as any); - // Clear values that are not yet present on the entity - const newKeys = Object.keys(omit(form.controls, Object.keys(entity))); - newKeys.forEach((key) => form.get(key).setValue(null)); + for (const key of Object.keys(form.controls)) { + form.get(key).setValue(entity[key]); + } + form.markAsPristine(); this.unsavedChanges.pending = false; } diff --git a/src/app/core/entity-details/form/form.component.spec.ts b/src/app/core/entity-details/form/form.component.spec.ts index ecdf23af5d..5b4de66acf 100644 --- a/src/app/core/entity-details/form/form.component.spec.ts +++ b/src/app/core/entity-details/form/form.component.spec.ts @@ -91,6 +91,6 @@ describe("FormComponent", () => { component.form.get("name").setValue("my name"); component.cancelClicked(); - expect(component.form.get("name")).toHaveValue(null); + expect(component.form.get("name").value).toBeUndefined(); }); });