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 19afa460c46..da045898edb 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 @@ -245,15 +245,7 @@ export class MaxDateFieldValidator extends BoundaryDateFieldValidator { } } -/** - * Validates the min constraint for the datetime value. - * - * Notes for developers: - * the format of the min/max values is always the ISO datetime: i.e. 2023-10-01T15:21:00.000Z. - * Min/Max values can be parsed with standard `new Date(value)` calls. - * - */ -export class MinDateTimeFieldValidator implements FormFieldValidator { +export abstract class BoundaryDateTimeFieldValidator implements FormFieldValidator { private supportedTypes = [FormFieldTypes.DATETIME]; isSupported(field: FormFieldModel): boolean { @@ -276,56 +268,64 @@ export class MinDateTimeFieldValidator implements FormFieldValidator { private checkDateTime(field: FormFieldModel): boolean { let isValid = true; const fieldValueDate = DateFnsUtils.getDate(field.value); - const min = DateFnsUtils.getDate(field.minValue); + const subjectFieldDate = DateFnsUtils.getDate(field[this.getSubjectField()]); - if (isBefore(fieldValueDate, min)) { - field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_LESS_THAN`; - field.validationSummary.attributes.set('minValue', DateFnsUtils.formatDate(min, field.dateDisplayFormat)); + if (this.compareDates(fieldValueDate, subjectFieldDate)) { + field.validationSummary.message = this.getErrorMessage(); + field.validationSummary.attributes.set(this.getSubjectField(), DateFnsUtils.formatDate(subjectFieldDate, field.dateDisplayFormat)); isValid = false; } return isValid; } + + protected abstract compareDates(fieldValueDate: Date, subjectFieldDate: Date): boolean; + + protected abstract getSubjectField(): string; + + protected abstract getErrorMessage(): string; } /** - * Validates the max constraint for the datetime value. + * Validates the min constraint for the datetime value. * * Notes for developers: * the format of the min/max values is always the ISO datetime: i.e. 2023-10-01T15:21:00.000Z. * Min/Max values can be parsed with standard `new Date(value)` calls. * */ -export class MaxDateTimeFieldValidator implements FormFieldValidator { - private supportedTypes = [FormFieldTypes.DATETIME]; +export class MinDateTimeFieldValidator extends BoundaryDateTimeFieldValidator { + protected compareDates(fieldValueDate: Date, subjectFieldDate: Date): boolean { + return isBefore(fieldValueDate, subjectFieldDate); + } - isSupported(field: FormFieldModel): boolean { - return field && this.supportedTypes.indexOf(field.type) > -1 && !!field.maxValue; + protected getSubjectField(): string { + return 'minValue'; } - validate(field: FormFieldModel): boolean { - let isValid = true; - if (this.isSupported(field) && field.value && field.isVisible) { - if (!DateTimeFieldValidator.isValidDateTime(field.value)) { - field.validationSummary.message = 'FORM.FIELD.VALIDATOR.INVALID_DATE'; - isValid = false; - } else { - isValid = this.checkDateTime(field); - } - } - return isValid; + protected getErrorMessage(): string { + return `FORM.FIELD.VALIDATOR.NOT_LESS_THAN`; } +} - private checkDateTime(field: FormFieldModel): boolean { - let isValid = true; - const fieldValueDate = DateFnsUtils.getDate(field.value); - const max = DateFnsUtils.getDate(field.maxValue); +/** + * Validates the max constraint for the datetime value. + * + * Notes for developers: + * the format of the min/max values is always the ISO datetime: i.e. 2023-10-01T15:21:00.000Z. + * Min/Max values can be parsed with standard `new Date(value)` calls. + * + */ +export class MaxDateTimeFieldValidator extends BoundaryDateTimeFieldValidator { + protected compareDates(fieldValueDate: Date, subjectFieldDate: Date): boolean { + return isAfter(fieldValueDate, subjectFieldDate); + } - if (isAfter(fieldValueDate, max)) { - field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_GREATER_THAN`; - field.validationSummary.attributes.set('maxValue', DateFnsUtils.formatDate(max, field.dateDisplayFormat)); - isValid = false; - } - return isValid; + protected getSubjectField(): string { + return 'maxValue'; + } + + protected getErrorMessage(): string { + return `FORM.FIELD.VALIDATOR.NOT_GREATER_THAN`; } }