From f49f6d1c04b5ccd78e57b3a9827efa4338e0d605 Mon Sep 17 00:00:00 2001 From: amentis Date: Tue, 23 Jan 2024 14:12:50 +0200 Subject: [PATCH] add dmp editor re apply validators --- .../dmp-editor-blueprint/dmp-editor.model.ts | 124 ++++++++++++++++-- 1 file changed, 115 insertions(+), 9 deletions(-) diff --git a/dmp-frontend/src/app/ui/dmp/dmp-editor-blueprint/dmp-editor.model.ts b/dmp-frontend/src/app/ui/dmp/dmp-editor-blueprint/dmp-editor.model.ts index c749896fd..b78cf9eb6 100644 --- a/dmp-frontend/src/app/ui/dmp/dmp-editor-blueprint/dmp-editor.model.ts +++ b/dmp-frontend/src/app/ui/dmp/dmp-editor-blueprint/dmp-editor.model.ts @@ -1,4 +1,4 @@ -import { FormControl, UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms"; +import { FormArray, FormControl, UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms"; import { DmpAccessType } from "@app/core/common/enum/dmp-access-type"; import { DmpStatus } from "@app/core/common/enum/dmp-status"; import { IsActive } from "@app/core/common/enum/is-active.enum"; @@ -33,7 +33,7 @@ export class DmpEditorModel extends BaseEditorModel implements DmpPersist { super.fromModel(item); this.label = item.label; this.status = item.status; - this.properties = new DmpPropertiesEditorModel().fromModel(item.properties, item.dmpReferences.filter(x => x.isActive === IsActive.Active), item.blueprint); + this.properties = new DmpPropertiesEditorModel(this.validationErrorModel).fromModel(item.properties, item.dmpReferences.filter(x => x.isActive === IsActive.Active), item.blueprint); this.description = item.description; this.language = item.language; this.blueprint = item.blueprint?.id; @@ -45,7 +45,7 @@ export class DmpEditorModel extends BaseEditorModel implements DmpPersist { if (sectionTempaltesFromDmp.length > 0) { item.dmpDescriptionTemplates?.filter(x => x.sectionId == section.id).forEach(dmpDescriptionTemplate => { - this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel().fromModel( + this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel(this.validationErrorModel).fromModel( { sectionId: section.id, descriptionTemplateGroupId: dmpDescriptionTemplate?.descriptionTemplateGroupId, @@ -53,14 +53,14 @@ export class DmpEditorModel extends BaseEditorModel implements DmpPersist { }); } else if (section.descriptionTemplates?.length > 0) { section.descriptionTemplates.forEach(blueprintDefinedDescriptionTemplate => { - this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel().fromModel( + this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel(this.validationErrorModel).fromModel( { sectionId: section.id, descriptionTemplateGroupId: blueprintDefinedDescriptionTemplate?.descriptionTemplateId, })); }); } else { - this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel().fromModel( + this.descriptionTemplates.push(new DmpDescriptionTemplateEditorModel(this.validationErrorModel).fromModel( { sectionId: section.id, })); @@ -120,6 +120,28 @@ export class DmpEditorModel extends BaseEditorModel implements DmpPersist { baseContext.validation = baseValidationArray; return baseContext; } + + static reApplyPropertiesValidators(params: { + formGroup: UntypedFormGroup, + validationErrorModel: ValidationErrorModel, + }): void { + + const { formGroup, validationErrorModel } = params; + const control = formGroup?.get('properties'); + DmpPropertiesEditorModel.reapplyValidators({ + formGroup: control as UntypedFormGroup, + rootPath: `properties.`, + validationErrorModel: validationErrorModel + }); + + (formGroup.get('descriptionTemplates') as FormArray).controls?.forEach( + (control, index) => DmpDescriptionTemplateEditorModel.reapplyValidators({ + formGroup: control as UntypedFormGroup, + rootPath: `descriptionTemplates[${index}].`, + validationErrorModel: validationErrorModel + }) + ); + } } export class DmpPropertiesEditorModel implements DmpPropertiesPersist { @@ -137,14 +159,14 @@ export class DmpPropertiesEditorModel implements DmpPropertiesPersist { dmpBlueprint.definition.sections.forEach(section => { section.fields.forEach(field => { - this.dmpBlueprintValues.set(field.id, new DmpBlueprintValueEditorModel().fromModel( + this.dmpBlueprintValues.set(field.id, new DmpBlueprintValueEditorModel(this.validationErrorModel).fromModel( { fieldId: field.id, fieldValue: item?.dmpBlueprintValues?.find(x => x.fieldId == field.id)?.fieldValue, }, dmpReferences)); }); }); - if (item?.contacts) { item.contacts.map(x => this.contacts.push(new DmpContactEditorModel().fromModel(x))); } + if (item?.contacts) { item.contacts.map(x => this.contacts.push(new DmpContactEditorModel(this.validationErrorModel).fromModel(x))); } return this; } @@ -168,7 +190,7 @@ export class DmpPropertiesEditorModel implements DmpPropertiesPersist { (item, index) => new DmpContactEditorModel( this.validationErrorModel ).fromModel(item).buildForm({ - rootPath: `contacts[${index}].` + rootPath: `${rootPath}contacts[${index}].` }), context.getValidation('contacts') ) ), @@ -178,7 +200,7 @@ export class DmpPropertiesEditorModel implements DmpPropertiesPersist { const dmpBlueprintValuesFormGroup = this.formBuilder.group({}); this.dmpBlueprintValues.forEach((value, key) => dmpBlueprintValuesFormGroup.addControl(key.toString(), value.buildForm({ - rootPath: `dmpBlueprintValues[${key}].` + rootPath: `${rootPath}dmpBlueprintValues[${key}].` })), context.getValidation('dmpBlueprintValues') ) formGroup.addControl('dmpBlueprintValues', dmpBlueprintValuesFormGroup); @@ -200,6 +222,33 @@ export class DmpPropertiesEditorModel implements DmpPropertiesPersist { baseContext.validation = baseValidationArray; return baseContext; } + + static reapplyValidators(params: { + formGroup: UntypedFormGroup, + validationErrorModel: ValidationErrorModel, + rootPath: string + }): void { + + const { formGroup, rootPath, validationErrorModel } = params; + const context = DmpPropertiesEditorModel.createValidationContext({ + rootPath, + validationErrorModel + }); + + ['dmpBlueprintValues'].forEach(keyField => { + const control = formGroup?.get(keyField); + control?.clearValidators(); + control?.addValidators(context.getValidation(keyField).validators); + }); + + (formGroup.get('contacts') as FormArray).controls?.forEach( + (control, index) => DmpContactEditorModel.reapplyValidators({ + formGroup: control as UntypedFormGroup, + rootPath: `${rootPath}contacts[${index}].`, + validationErrorModel: validationErrorModel + }) + ); + } } export class DmpBlueprintValueEditorModel implements DmpBlueprintValuePersist { @@ -256,6 +305,25 @@ export class DmpBlueprintValueEditorModel implements DmpBlueprintValuePersist { baseContext.validation = baseValidationArray; return baseContext; } + + static reapplyValidators(params: { + formGroup: UntypedFormGroup, + validationErrorModel: ValidationErrorModel, + rootPath: string + }): void { + + const { formGroup, rootPath, validationErrorModel } = params; + const context = DmpBlueprintValueEditorModel.createValidationContext({ + rootPath, + validationErrorModel + }); + + ['fieldId', 'fieldValue', 'references'].forEach(keyField => { + const control = formGroup?.get(keyField); + control?.clearValidators(); + control?.addValidators(context.getValidation(keyField).validators); + }) + } } export class DmpContactEditorModel implements DmpContactPersist { @@ -316,6 +384,25 @@ export class DmpContactEditorModel implements DmpContactPersist { baseContext.validation = baseValidationArray; return baseContext; } + + static reapplyValidators(params: { + formGroup: UntypedFormGroup, + validationErrorModel: ValidationErrorModel, + rootPath: string + }): void { + + const { formGroup, rootPath, validationErrorModel } = params; + const context = DmpContactEditorModel.createValidationContext({ + rootPath, + validationErrorModel + }); + + ['userId', 'firstName', 'lastName', 'email'].forEach(keyField => { + const control = formGroup?.get(keyField); + control?.clearValidators(); + control?.addValidators(context.getValidation(keyField).validators); + }) + } } export class DmpReferenceEditorModel implements DmpReferencePersist { @@ -429,4 +516,23 @@ export class DmpDescriptionTemplateEditorModel implements DmpDescriptionTemplate baseContext.validation = baseValidationArray; return baseContext; } + + static reapplyValidators(params: { + formGroup: UntypedFormGroup, + validationErrorModel: ValidationErrorModel, + rootPath: string + }): void { + + const { formGroup, rootPath, validationErrorModel } = params; + const context = DmpDescriptionTemplateEditorModel.createValidationContext({ + rootPath, + validationErrorModel + }); + + ['descriptionTemplateGroupId', 'sectionId'].forEach(keyField => { + const control = formGroup?.get(keyField); + control?.clearValidators(); + control?.addValidators(context.getValidation(keyField).validators); + }) + } } \ No newline at end of file