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
This commit is contained in:
Bartosz Sekula
2025-09-02 11:51:13 +02:00
committed by GitHub
parent a4e62974ea
commit aae4efdd92
3 changed files with 54 additions and 5 deletions

View File

@@ -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', () => {

View File

@@ -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(),

View File

@@ -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', () => {