validators remove logic

This commit is contained in:
Diamantis Tziotzios 2024-01-15 13:33:49 +02:00
parent 0d65c38668
commit 0e61925f8d
2 changed files with 57 additions and 1 deletions

View File

@ -227,6 +227,15 @@ export class ReferenceTypeEditorComponent extends BaseEditor<ReferenceTypeEditor
(this.formGroup.get('definition').get('fields') as FormArray).removeAt(fieldIndex);
//Reapply validators
ReferenceTypeEditorModel.reApplyDefinitionFieldsValidators(
{
formGroup: this.formGroup,
validationErrorModel: this.editorModel.validationErrorModel
}
)
this.formGroup.get('definition').get('fields').markAsDirty();
const sourceFormArray = ((this.formGroup.get('definition').get('sources') as FormArray));
for (let i = 0; i < sourceFormArray.length; i++) {

View File

@ -1,4 +1,4 @@
import { UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms";
import { UntypedFormArray, UntypedFormBuilder, UntypedFormGroup, Validators } from "@angular/forms";
import { ReferenceFieldDataType } from "@app/core/common/enum/reference-field-data-type";
import { ReferenceTypeExternalApiHTTPMethodType } from "@app/core/common/enum/reference-type-external-api-http-method-type";
import { ReferenceTypeSourceType } from "@app/core/common/enum/reference-type-source-type";
@ -63,6 +63,20 @@ export class ReferenceTypeEditorModel extends BaseEditorModel implements Referen
const field: ReferenceTypeFieldEditorModel = new ReferenceTypeFieldEditorModel(this.validationErrorModel);
return field.buildForm({ rootPath: 'definition.fields[' + index + '].' });
}
static reApplyDefinitionFieldsValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
}): void {
const { formGroup, validationErrorModel } = params;
const control = formGroup?.get('definition');
ReferenceTypeDefinitionEditorModel.reapplyFieldsValidators({
formArray: control.get('fields') as UntypedFormArray,
rootPath: `definition.`,
validationErrorModel: validationErrorModel
});
}
}
export class ReferenceTypeDefinitionEditorModel implements ReferenceTypeDefinitionPersist {
@ -133,6 +147,21 @@ export class ReferenceTypeDefinitionEditorModel implements ReferenceTypeDefiniti
return baseContext;
}
static reapplyFieldsValidators(params: {
formArray: UntypedFormArray,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { validationErrorModel, rootPath, formArray } = params;
formArray?.controls?.forEach(
(control, index) => ReferenceTypeFieldEditorModel.reapplyValidators({
formGroup: control as UntypedFormGroup,
rootPath: `${rootPath}fields[${index}].`,
validationErrorModel: validationErrorModel
})
);
}
}
export class ReferenceTypeFieldEditorModel implements ReferenceTypeFieldPersist {
@ -195,6 +224,24 @@ export class ReferenceTypeFieldEditorModel implements ReferenceTypeFieldPersist
return baseContext;
}
static reapplyValidators(params: {
formGroup: UntypedFormGroup,
validationErrorModel: ValidationErrorModel,
rootPath: string
}): void {
const { formGroup, rootPath, validationErrorModel } = params;
const context = ReferenceTypeFieldEditorModel.createValidationContext({
rootPath,
validationErrorModel
});
['code', 'label', 'description', 'dataType'].forEach(keyField => {
const control = formGroup?.get(keyField);
control?.clearValidators();
control?.addValidators(context.getValidation(keyField).validators);
})
}
}
export class ReferenceTypeSourceBaseConfigurationEditorModel implements ReferenceTypeSourceBaseConfigurationPersist {