mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-09-17 14:21:29 +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(validator.validate(field)).toBe(false);
|
||||||
expect(field.validationSummary).not.toBeNull();
|
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', () => {
|
describe('MinValueFieldValidator', () => {
|
||||||
|
@@ -129,23 +129,33 @@ export class MinLengthFieldValidator implements FormFieldValidator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export class MaxLengthFieldValidator 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 {
|
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 {
|
validate(field: FormFieldModel): boolean {
|
||||||
if (this.isSupported(field) && field.value && field.isVisible) {
|
if (this.isSupported(field) && field.value && field.isVisible) {
|
||||||
if (field.value.length <= field.maxLength) {
|
if (field.value.toString().length <= this.getMaxLength(field)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NO_LONGER_THAN`;
|
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 false;
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getMaxLength(field: FormFieldModel): number | undefined {
|
||||||
|
return this.maxLength ?? field.maxLength;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export class MinValueFieldValidator implements FormFieldValidator {
|
export class MinValueFieldValidator implements FormFieldValidator {
|
||||||
@@ -299,6 +309,7 @@ export const FORM_FIELD_VALIDATORS = [
|
|||||||
new NumberFieldValidator(),
|
new NumberFieldValidator(),
|
||||||
new MinLengthFieldValidator(),
|
new MinLengthFieldValidator(),
|
||||||
new MaxLengthFieldValidator(),
|
new MaxLengthFieldValidator(),
|
||||||
|
new MaxLengthFieldValidator([FormFieldTypes.NUMBER], 10),
|
||||||
new MinValueFieldValidator(),
|
new MinValueFieldValidator(),
|
||||||
new MaxValueFieldValidator(),
|
new MaxValueFieldValidator(),
|
||||||
new RegExFieldValidator(),
|
new RegExFieldValidator(),
|
||||||
|
@@ -1210,7 +1210,7 @@ describe('FormCloudComponent', () => {
|
|||||||
formComponent.formCloudRepresentationJSON = new FormCloudRepresentation(JSON.parse(JSON.stringify(cloudFormMock)));
|
formComponent.formCloudRepresentationJSON = new FormCloudRepresentation(JSON.parse(JSON.stringify(cloudFormMock)));
|
||||||
const form = formComponent.parseForm(formComponent.formCloudRepresentationJSON);
|
const form = formComponent.parseForm(formComponent.formCloudRepresentationJSON);
|
||||||
expect(formComponent.fieldValidators.length).toBe(1);
|
expect(formComponent.fieldValidators.length).toBe(1);
|
||||||
expect(form.fieldValidators.length).toBe(10);
|
expect(form.fieldValidators.length).toBe(11);
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('form validations', () => {
|
describe('form validations', () => {
|
||||||
|
Reference in New Issue
Block a user