mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-09-10 14:11:42 +00:00
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:
@@ -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', () => {
|
||||
|
@@ -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(),
|
||||
|
Reference in New Issue
Block a user