fix validators to description template and dmp blueprint

This commit is contained in:
amentis 2024-04-04 18:35:24 +03:00
parent f7aaa04c0c
commit 2b2e04012d
6 changed files with 42 additions and 7 deletions

View File

@ -160,7 +160,10 @@ public class SectionPersist {
.iff(() -> !this.isListNullOrEmpty(item.getFieldSets())) .iff(() -> !this.isListNullOrEmpty(item.getFieldSets()))
.on(SectionPersist._fieldSets) .on(SectionPersist._fieldSets)
.over(item.getFieldSets()) .over(item.getFieldSets())
.using((itm) -> this.validatorFactory.validator(FieldSetPersist.FieldSetPersistValidator.class)) .using((itm) -> this.validatorFactory.validator(FieldSetPersist.FieldSetPersistValidator.class)),
this.spec()
.must(() -> !this.isListNullOrEmpty(item.getFieldSets()) || !this.isListNullOrEmpty(item.getSections()))
.failOn(SectionPersist._fieldSets).failWith(messageSource.getMessage("Validation_Required", new Object[]{SectionPersist._fieldSets}, LocaleContextHolder.getLocale()))
); );
} }
} }

View File

@ -12,6 +12,7 @@ import { BackendErrorValidator } from "@common/forms/validation/custom-validator
import { ValidationErrorModel } from "@common/forms/validation/error-model/validation-error-model"; import { ValidationErrorModel } from "@common/forms/validation/error-model/validation-error-model";
import { Validation, ValidationContext } from "@common/forms/validation/validation-context"; import { Validation, ValidationContext } from "@common/forms/validation/validation-context";
import { Guid } from "@common/types/guid"; import { Guid } from "@common/types/guid";
import { EditorCustomValidators } from "./custom-validators/editor-custom-validators";
export class DescriptionTemplateEditorModel extends BaseEditorModel implements DescriptionTemplatePersist { export class DescriptionTemplateEditorModel extends BaseEditorModel implements DescriptionTemplatePersist {
label: string; label: string;
@ -399,7 +400,7 @@ export class DescriptionTemplateSectionEditorModel implements DescriptionTemplat
}); });
} }
return this.formBuilder.group({ const formGroup= this.formBuilder.group({
id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators], id: [{ value: this.id, disabled: disabled }, context.getValidation('id').validators],
ordinal: [{ value: this.ordinal, disabled: disabled }, context.getValidation('ordinal').validators], ordinal: [{ value: this.ordinal, disabled: disabled }, context.getValidation('ordinal').validators],
defaultVisibility: [{ value: this.defaultVisibility, disabled: disabled }, context.getValidation('defaultVisibility').validators], defaultVisibility: [{ value: this.defaultVisibility, disabled: disabled }, context.getValidation('defaultVisibility').validators],
@ -421,6 +422,10 @@ export class DescriptionTemplateSectionEditorModel implements DescriptionTemplat
), context.getValidation('fieldSets').validators ), context.getValidation('fieldSets').validators
) )
}); });
formGroup.setValidators(EditorCustomValidators.sectionHasAtLeastOneChildOf('fieldSets','sections'));
return formGroup;
} }
static createValidationContext(params: { static createValidationContext(params: {

View File

@ -39,8 +39,9 @@
<mat-error *ngIf="formGroup.get('label').hasError('required')">{{'GENERAL.VALIDATION.REQUIRED' | translate}}</mat-error> <mat-error *ngIf="formGroup.get('label').hasError('required')">{{'GENERAL.VALIDATION.REQUIRED' | translate}}</mat-error>
</mat-form-field> </mat-form-field>
</div> </div>
<h4 class="col-12">{{'DMP-BLUEPRINT-EDITOR.FIELDS.SECTIONS' | translate}}</h4> <h4 class="col-12">{{'DMP-BLUEPRINT-EDITOR.FIELDS.SECTIONS' | translate}}
<mat-error *ngIf="formGroup.get('definition').get('sections').hasError('dmpBlueprintSystemFieldRequired')"> {{'DMP-BLUEPRINT-EDITOR.SYSTEM-FIELDS-REQUIRED' | translate}} </mat-error>
</h4>
<div class="col-12" cdkDropList (cdkDropListDropped)="dropSections($event)"> <div class="col-12" cdkDropList (cdkDropListDropped)="dropSections($event)">
<div *ngFor="let section of formGroup.get('definition').get('sections').controls; let sectionIndex=index;" class="row mb-3" cdkDrag [cdkDragDisabled]="formGroup.disabled"> <div *ngFor="let section of formGroup.get('definition').get('sections').controls; let sectionIndex=index;" class="row mb-3" cdkDrag [cdkDragDisabled]="formGroup.disabled">
<div class="col-12"> <div class="col-12">

View File

@ -5,7 +5,7 @@ import { DmpBlueprintStatus } from "@app/core/common/enum/dmp-blueprint-status";
import { DmpBlueprintSystemFieldType } from "@app/core/common/enum/dmp-blueprint-system-field-type"; import { DmpBlueprintSystemFieldType } from "@app/core/common/enum/dmp-blueprint-system-field-type";
import { DescriptionTemplatesInSection, DescriptionTemplatesInSectionPersist, DmpBlueprint, DmpBlueprintDefinition, DmpBlueprintDefinitionPersist, DmpBlueprintDefinitionSection, DmpBlueprintDefinitionSectionPersist, DmpBlueprintPersist, ExtraFieldInSection, FieldInSection, FieldInSectionPersist, ReferenceTypeFieldInSection, SystemFieldInSection } from "@app/core/model/dmp-blueprint/dmp-blueprint"; import { DescriptionTemplatesInSection, DescriptionTemplatesInSectionPersist, DmpBlueprint, DmpBlueprintDefinition, DmpBlueprintDefinitionPersist, DmpBlueprintDefinitionSection, DmpBlueprintDefinitionSectionPersist, DmpBlueprintPersist, ExtraFieldInSection, FieldInSection, FieldInSectionPersist, ReferenceTypeFieldInSection, SystemFieldInSection } from "@app/core/model/dmp-blueprint/dmp-blueprint";
import { BaseEditorModel } from "@common/base/base-form-editor-model"; import { BaseEditorModel } from "@common/base/base-form-editor-model";
import { BackendErrorValidator } from "@common/forms/validation/custom-validator"; import { BackendErrorValidator, DmpBlueprintSystemFieldRequiredValidator } from "@common/forms/validation/custom-validator";
import { ValidationErrorModel } from "@common/forms/validation/error-model/validation-error-model"; import { ValidationErrorModel } from "@common/forms/validation/error-model/validation-error-model";
import { Validation, ValidationContext } from "@common/forms/validation/validation-context"; import { Validation, ValidationContext } from "@common/forms/validation/validation-context";
import { Guid } from "@common/types/guid"; import { Guid } from "@common/types/guid";
@ -142,7 +142,7 @@ export class DmpBlueprintDefinitionEditorModel implements DmpBlueprintDefinition
const baseContext: ValidationContext = new ValidationContext(); const baseContext: ValidationContext = new ValidationContext();
const baseValidationArray: Validation[] = new Array<Validation>(); const baseValidationArray: Validation[] = new Array<Validation>();
baseValidationArray.push({ key: 'sections', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}sections`)] }); baseValidationArray.push({ key: 'sections', validators: [Validators.required, BackendErrorValidator(validationErrorModel, `${rootPath}sections`), DmpBlueprintSystemFieldRequiredValidator()] });
baseContext.validation = baseValidationArray; baseContext.validation = baseValidationArray;
return baseContext; return baseContext;

View File

@ -1274,6 +1274,7 @@
}, },
"SECTIONS-REQUIRED": "Required", "SECTIONS-REQUIRED": "Required",
"FIELDS-REQUIRED": "Required", "FIELDS-REQUIRED": "Required",
"SYSTEM-FIELDS-REQUIRED": "Title and Description are required",
"FIELDS": { "FIELDS": {
"NAME": "Name", "NAME": "Name",
"SECTIONS": "Sections", "SECTIONS": "Sections",

View File

@ -1,6 +1,7 @@
import { AbstractControl, ValidatorFn, Validators } from '@angular/forms'; import { AbstractControl, UntypedFormArray, UntypedFormGroup, ValidatorFn, Validators } from '@angular/forms';
import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model'; import { ValidationErrorModel } from '@common/forms/validation/error-model/validation-error-model';
import { isNullOrUndefined } from '@app/utilities/enhancers/utils'; import { isNullOrUndefined } from '@app/utilities/enhancers/utils';
import { DmpBlueprintSystemFieldType } from '@app/core/common/enum/dmp-blueprint-system-field-type';
export function BackendErrorValidator(errorModel: ValidationErrorModel, propertyName: string): ValidatorFn { export function BackendErrorValidator(errorModel: ValidationErrorModel, propertyName: string): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => { return (control: AbstractControl): { [key: string]: any } => {
@ -98,3 +99,27 @@ export function PasswordMatchValidator(passwordControlName: string, repeatPasswo
return null; return null;
}; };
} }
export function DmpBlueprintSystemFieldRequiredValidator(): ValidatorFn {
return (control: AbstractControl): { [key: string]: any } => {
let foundTitle = false;
let foundDescription = false;
const sectionsFormArray = (control as UntypedFormArray);
if (sectionsFormArray.controls != null && sectionsFormArray.controls.length > 0 ){
sectionsFormArray.controls.forEach((section, index) => {
const fieldsFormArray = section.get('fields') as UntypedFormArray;
if (fieldsFormArray && fieldsFormArray.length > 0){
if (fieldsFormArray.controls.some(y => (y as UntypedFormGroup).get('systemFieldType')?.value === DmpBlueprintSystemFieldType.Title)){
foundTitle = true;
}
if (fieldsFormArray.controls.some(y => (y as UntypedFormGroup).get('systemFieldType')?.value === DmpBlueprintSystemFieldType.Description)){
foundDescription = true;
}
}
});
}
return foundTitle && foundDescription ? null : { 'dmpBlueprintSystemFieldRequired': true };
};
}