mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2025-07-24 17:32:15 +00:00
[ADF-4775] Make date widget compatible with APS1 and APS2 (#4966)
* [ADF-4775] Make date widget compatible with APS1 and APS2 * Refactor and create new cloud widget
This commit is contained in:
committed by
Eugenio Romano
parent
bf828b6389
commit
8adb9b2a25
@@ -28,7 +28,9 @@ import {
|
||||
RegExFieldValidator,
|
||||
RequiredFieldValidator,
|
||||
MaxDateTimeFieldValidator,
|
||||
MinDateTimeFieldValidator
|
||||
MinDateTimeFieldValidator,
|
||||
MaxDateFieldValidator,
|
||||
MinDateFieldValidator
|
||||
} from './form-field-validator';
|
||||
import { FormFieldModel } from './form-field.model';
|
||||
import { FormModel } from './form.model';
|
||||
@@ -868,4 +870,192 @@ describe('FormFieldValidator', () => {
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('MaxDateFieldValidator', () => {
|
||||
|
||||
let validator: MaxDateFieldValidator;
|
||||
|
||||
beforeEach(() => {
|
||||
validator = new MaxDateFieldValidator();
|
||||
});
|
||||
|
||||
it('should require maxValue defined', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE
|
||||
});
|
||||
expect(validator.isSupported(field)).toBeFalsy();
|
||||
|
||||
field.maxValue = '9999-02-08';
|
||||
expect(validator.isSupported(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should support date widgets only', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
maxValue: '9999-02-08'
|
||||
});
|
||||
|
||||
expect(validator.isSupported(field)).toBeTruthy();
|
||||
|
||||
field.type = FormFieldTypes.TEXT;
|
||||
expect(validator.isSupported(field)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should allow empty values', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: null,
|
||||
maxValue: '9999-02-08'
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should succeed for unsupported types', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.TEXT
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should succeed validating value checking the date', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
maxValue: '9999-02-09'
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should fail validating value checking the date', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
maxValue: '9999-02-07'
|
||||
});
|
||||
|
||||
field.validationSummary = new ErrorMessageModel();
|
||||
expect(validator.validate(field)).toBeFalsy();
|
||||
expect(field.validationSummary).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should validate with APS1 format', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
maxValue: '09-02-9999'
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should fail validating with APS1 format', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
maxValue: '07-02-9999'
|
||||
});
|
||||
|
||||
field.validationSummary = new ErrorMessageModel();
|
||||
expect(validator.validate(field)).toBeFalsy();
|
||||
expect(field.validationSummary).not.toBeNull();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
describe('MinDateFieldValidator', () => {
|
||||
|
||||
let validator: MinDateFieldValidator;
|
||||
|
||||
beforeEach(() => {
|
||||
validator = new MinDateFieldValidator();
|
||||
});
|
||||
|
||||
it('should require maxValue defined', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE
|
||||
});
|
||||
expect(validator.isSupported(field)).toBeFalsy();
|
||||
|
||||
field.minValue = '9999-02-08';
|
||||
expect(validator.isSupported(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should support date widgets only', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
minValue: '9999-02-08'
|
||||
});
|
||||
|
||||
expect(validator.isSupported(field)).toBeTruthy();
|
||||
|
||||
field.type = FormFieldTypes.TEXT;
|
||||
expect(validator.isSupported(field)).toBeFalsy();
|
||||
});
|
||||
|
||||
it('should allow empty values', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: null,
|
||||
minValue: '9999-02-08'
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should succeed for unsupported types', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.TEXT
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should succeed validating value checking the date', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
minValue: '9999-02-07'
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should fail validating value checking the date', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
minValue: '9999-02-09'
|
||||
});
|
||||
|
||||
field.validationSummary = new ErrorMessageModel();
|
||||
expect(validator.validate(field)).toBeFalsy();
|
||||
expect(field.validationSummary).not.toBeNull();
|
||||
});
|
||||
|
||||
it('should validate with APS1 format', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
minValue: '07-02-9999'
|
||||
});
|
||||
|
||||
expect(validator.validate(field)).toBeTruthy();
|
||||
});
|
||||
|
||||
it('should fail validating with APS1 format', () => {
|
||||
const field = new FormFieldModel(new FormModel(), {
|
||||
type: FormFieldTypes.DATE,
|
||||
value: '9999-02-08T00:00:00',
|
||||
minValue: '09-02-9999'
|
||||
});
|
||||
|
||||
field.validationSummary = new ErrorMessageModel();
|
||||
expect(validator.validate(field)).toBeFalsy();
|
||||
expect(field.validationSummary).not.toBeNull();
|
||||
});
|
||||
|
||||
});
|
||||
});
|
||||
|
@@ -165,17 +165,15 @@ export class DateFieldValidator implements FormFieldValidator {
|
||||
}
|
||||
}
|
||||
|
||||
export class MinDateFieldValidator implements FormFieldValidator {
|
||||
export abstract class BoundaryDateFieldValidator implements FormFieldValidator {
|
||||
|
||||
private supportedTypes = [
|
||||
DATE_FORMAT_CLOUD = 'YYYY-MM-DD';
|
||||
DATE_FORMAT = 'DD-MM-YYYY';
|
||||
|
||||
supportedTypes = [
|
||||
FormFieldTypes.DATE
|
||||
];
|
||||
|
||||
isSupported(field: FormFieldModel): boolean {
|
||||
return field &&
|
||||
this.supportedTypes.indexOf(field.type) > -1 && !!field.minValue;
|
||||
}
|
||||
|
||||
validate(field: FormFieldModel): boolean {
|
||||
let isValid = true;
|
||||
if (this.isSupported(field) && field.value && field.isVisible) {
|
||||
@@ -191,8 +189,20 @@ export class MinDateFieldValidator implements FormFieldValidator {
|
||||
return isValid;
|
||||
}
|
||||
|
||||
private checkDate(field: FormFieldModel, dateFormat: string): boolean {
|
||||
const MIN_DATE_FORMAT = 'DD-MM-YYYY';
|
||||
extractDateFormat(date: string): string {
|
||||
const brokenDownDate = date.split('-');
|
||||
return brokenDownDate[0].length === 4 ? this.DATE_FORMAT_CLOUD : this.DATE_FORMAT;
|
||||
}
|
||||
|
||||
abstract checkDate(field: FormFieldModel, dateFormat: string);
|
||||
abstract isSupported(field: FormFieldModel);
|
||||
|
||||
}
|
||||
|
||||
export class MinDateFieldValidator extends BoundaryDateFieldValidator {
|
||||
|
||||
checkDate(field: FormFieldModel, dateFormat: string): boolean {
|
||||
|
||||
let isValid = true;
|
||||
// remove time and timezone info
|
||||
let fieldValueData;
|
||||
@@ -201,7 +211,9 @@ export class MinDateFieldValidator implements FormFieldValidator {
|
||||
} else {
|
||||
fieldValueData = field.value;
|
||||
}
|
||||
const min = moment(field.minValue, MIN_DATE_FORMAT);
|
||||
|
||||
const minValueDateFormat = this.extractDateFormat(field.minValue);
|
||||
const min = moment(field.minValue, minValueDateFormat);
|
||||
|
||||
if (fieldValueData.isBefore(min)) {
|
||||
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_LESS_THAN`;
|
||||
@@ -210,47 +222,41 @@ export class MinDateFieldValidator implements FormFieldValidator {
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
isSupported(field: FormFieldModel): boolean {
|
||||
return field &&
|
||||
this.supportedTypes.indexOf(field.type) > -1 && !!field.minValue;
|
||||
}
|
||||
}
|
||||
|
||||
export class MaxDateFieldValidator implements FormFieldValidator {
|
||||
export class MaxDateFieldValidator extends BoundaryDateFieldValidator {
|
||||
|
||||
MAX_DATE_FORMAT = 'DD-MM-YYYY';
|
||||
checkDate(field: FormFieldModel, dateFormat: string): boolean {
|
||||
|
||||
private supportedTypes = [
|
||||
FormFieldTypes.DATE
|
||||
];
|
||||
let isValid = true;
|
||||
// remove time and timezone info
|
||||
let fieldValueData;
|
||||
if (typeof field.value === 'string') {
|
||||
fieldValueData = moment(field.value.split('T')[0], dateFormat);
|
||||
} else {
|
||||
fieldValueData = field.value;
|
||||
}
|
||||
|
||||
const maxValueDateFormat = this.extractDateFormat(field.maxValue);
|
||||
const max = moment(field.maxValue, maxValueDateFormat);
|
||||
|
||||
if (fieldValueData.isAfter(max)) {
|
||||
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_GREATER_THAN`;
|
||||
field.validationSummary.attributes.set('maxValue', max.format(field.dateDisplayFormat).toLocaleUpperCase());
|
||||
isValid = false;
|
||||
}
|
||||
return isValid;
|
||||
}
|
||||
|
||||
isSupported(field: FormFieldModel): boolean {
|
||||
return field &&
|
||||
this.supportedTypes.indexOf(field.type) > -1 && !!field.maxValue;
|
||||
}
|
||||
|
||||
validate(field: FormFieldModel): boolean {
|
||||
if (this.isSupported(field) && field.value && field.isVisible) {
|
||||
const dateFormat = field.dateDisplayFormat;
|
||||
|
||||
if (!DateFieldValidator.isValidDate(field.value, dateFormat)) {
|
||||
field.validationSummary.message = 'FORM.FIELD.VALIDATOR.INVALID_DATE';
|
||||
return false;
|
||||
}
|
||||
|
||||
// remove time and timezone info
|
||||
let d;
|
||||
if (typeof field.value === 'string') {
|
||||
d = moment(field.value.split('T')[0], dateFormat);
|
||||
} else {
|
||||
d = field.value;
|
||||
}
|
||||
const max = moment(field.maxValue, this.MAX_DATE_FORMAT);
|
||||
|
||||
if (d.isAfter(max)) {
|
||||
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_GREATER_THAN`;
|
||||
field.validationSummary.attributes.set('maxValue', max.format(field.dateDisplayFormat).toLocaleUpperCase());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
export class MinDateTimeFieldValidator implements FormFieldValidator {
|
||||
|
@@ -41,7 +41,7 @@ import { takeUntil } from 'rxjs/operators';
|
||||
})
|
||||
export class DateWidgetComponent extends WidgetComponent implements OnInit, OnDestroy {
|
||||
|
||||
DATE_FORMAT = 'DD/MM/YYYY';
|
||||
DATE_FORMAT = 'DD-MM-YYYY';
|
||||
|
||||
minDate: Moment;
|
||||
maxDate: Moment;
|
||||
|
Reference in New Issue
Block a user