From aae4efdd92a0454c37343b773a09016364d4cde5 Mon Sep 17 00:00:00 2001 From: Bartosz Sekula Date: Tue, 2 Sep 2025 11:51:13 +0200 Subject: [PATCH] AAE-30058 Integer form field should not allow more than 10 digits (#11146) * AAE-30058 Integer form field should not allow more than 10 digits * Update maxLength validator to get custom values * fix units --- .../widgets/core/form-field-validator.spec.ts | 38 +++++++++++++++++++ .../widgets/core/form-field-validator.ts | 19 ++++++++-- .../components/form-cloud.component.spec.ts | 2 +- 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts b/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts index b55ca856c8..69761d3718 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-validator.spec.ts @@ -366,6 +366,44 @@ describe('FormFieldValidator', () => { expect(validator.validate(field)).toBe(false); expect(field.validationSummary).not.toBeNull(); }); + + describe('MaxLengthFieldValidator with custom value', () => { + let customValidator: MaxLengthFieldValidator; + + beforeEach(() => { + customValidator = new MaxLengthFieldValidator([FormFieldTypes.NUMBER], 3); + }); + + it('should validate integer values', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.NUMBER, + value: '444' + }); + + const isValid = customValidator.validate(field); + expect(isValid).toBe(true); + }); + + it('should validate values exceeding maxLength', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.NUMBER, + value: '4444' + }); + + const isValid = customValidator.validate(field); + expect(isValid).toBe(false); + }); + + it('should not validate not supported fields', () => { + const field = new FormFieldModel(new FormModel(), { + type: FormFieldTypes.TEXT, + value: 'abcd' + }); + + const isSupported = customValidator.isSupported(field); + expect(isSupported).toBe(false); + }); + }); }); describe('MinValueFieldValidator', () => { diff --git a/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts b/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts index 0993927ba8..97a399967f 100644 --- a/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts +++ b/lib/core/src/lib/form/components/widgets/core/form-field-validator.ts @@ -129,23 +129,33 @@ export class MinLengthFieldValidator implements FormFieldValidator { } export class MaxLengthFieldValidator implements FormFieldValidator { - private supportedTypes = [FormFieldTypes.TEXT, FormFieldTypes.MULTILINE_TEXT]; + constructor( + private supportedTypes: FormFieldTypes[] = [FormFieldTypes.TEXT, FormFieldTypes.MULTILINE_TEXT], + private maxLength?: number + ) {} isSupported(field: FormFieldModel): boolean { - return field && this.supportedTypes.indexOf(field.type) > -1 && field.maxLength > 0; + return field && this.supportedTypes.indexOf(field.type) > -1 && this.getMaxLength(field) > 0; } validate(field: FormFieldModel): boolean { if (this.isSupported(field) && field.value && field.isVisible) { - if (field.value.length <= field.maxLength) { + if (field.value.toString().length <= this.getMaxLength(field)) { return true; } + field.validationSummary.message = `FORM.FIELD.VALIDATOR.NO_LONGER_THAN`; - field.validationSummary.attributes.set('maxLength', field.maxLength.toLocaleString()); + field.validationSummary.attributes.set('maxLength', this.getMaxLength(field).toLocaleString()); + return false; } + return true; } + + getMaxLength(field: FormFieldModel): number | undefined { + return this.maxLength ?? field.maxLength; + } } export class MinValueFieldValidator implements FormFieldValidator { @@ -299,6 +309,7 @@ export const FORM_FIELD_VALIDATORS = [ new NumberFieldValidator(), new MinLengthFieldValidator(), new MaxLengthFieldValidator(), + new MaxLengthFieldValidator([FormFieldTypes.NUMBER], 10), new MinValueFieldValidator(), new MaxValueFieldValidator(), new RegExFieldValidator(), diff --git a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts index 59d7b5ebfb..6f2be25c51 100644 --- a/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts +++ b/lib/process-services-cloud/src/lib/form/components/form-cloud.component.spec.ts @@ -1210,7 +1210,7 @@ describe('FormCloudComponent', () => { formComponent.formCloudRepresentationJSON = new FormCloudRepresentation(JSON.parse(JSON.stringify(cloudFormMock))); const form = formComponent.parseForm(formComponent.formCloudRepresentationJSON); expect(formComponent.fieldValidators.length).toBe(1); - expect(form.fieldValidators.length).toBe(10); + expect(form.fieldValidators.length).toBe(11); }); describe('form validations', () => {