Skip to content

Commit

Permalink
AAE-23520 Fix date parse with invalid display format
Browse files Browse the repository at this point in the history
  • Loading branch information
pmartinezga authored and BSekula committed Jul 24, 2024
1 parent c070a5e commit 888b5fc
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,9 @@ export class ErrorMessageModel {
getAttributesAsJsonObj() {
let result = {};
if (this.attributes.size > 0) {
const obj = Object.create(null);
this.attributes.forEach((value, key) => {
obj[key] = value;
result[key] = typeof value === 'string' ? value : JSON.stringify(value);
});
result = JSON.stringify(obj);
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ export class MinDateTimeFieldValidator implements FormFieldValidator {

if (isBefore(fieldValueDate, min)) {
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_LESS_THAN`;
field.validationSummary.attributes.set('minValue', DateFnsUtils.formatDate(min, field.dateDisplayFormat).replace(':', '-'));
field.validationSummary.attributes.set('minValue', DateFnsUtils.formatDate(DateFnsUtils.utcToLocal(min), field.dateDisplayFormat));
isValid = false;
}
return isValid;
Expand Down Expand Up @@ -322,7 +322,7 @@ export class MaxDateTimeFieldValidator implements FormFieldValidator {

if (isAfter(fieldValueDate, max)) {
field.validationSummary.message = `FORM.FIELD.VALIDATOR.NOT_GREATER_THAN`;
field.validationSummary.attributes.set('maxValue', DateFnsUtils.formatDate(max, field.dateDisplayFormat).replace(':', '-'));
field.validationSummary.attributes.set('maxValue', DateFnsUtils.formatDate(DateFnsUtils.utcToLocal(max), field.dateDisplayFormat));
isValid = false;
}
return isValid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ export class FormFieldModel extends FormWidgetModel {
}

parseValue(json: any): any {
let value = Object.prototype.hasOwnProperty.call(json, 'value') && json.value !== undefined ? json.value : null;
const value = Object.prototype.hasOwnProperty.call(json, 'value') && json.value !== undefined ? json.value : null;

/*
This is needed due to Activiti issue related to reading dropdown values as value string
Expand Down Expand Up @@ -440,7 +440,12 @@ export class FormFieldModel extends FormWidgetModel {
this.value = new Date();
}

const dateValue = DateFnsUtils.parseDate(this.value, this.dateDisplayFormat);
let dateValue;
try {
dateValue = DateFnsUtils.parseDate(this.value, this.dateDisplayFormat);
} catch (e) {
dateValue = new Date('error');
}

if (isValidDate(dateValue)) {
const datePart = DateFnsUtils.formatDate(dateValue, 'yyyy-MM-dd');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit {

if (this.field) {
if (this.field.minValue) {
this.minDate = DateFnsUtils.localToUtc(new Date(this.field.minValue));
this.minDate = DateFnsUtils.utcToLocal(new Date(this.field.minValue));
}

if (this.field.maxValue) {
this.maxDate = DateFnsUtils.localToUtc(new Date(this.field.maxValue));
this.maxDate = DateFnsUtils.utcToLocal(new Date(this.field.maxValue));
}

if (this.field.value) {
this.value = DateFnsUtils.localToUtc(new Date(this.field.value));
this.value = new Date(this.field.value);
}
}
}
Expand All @@ -85,7 +85,7 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit {
const newValue = this.dateTimeAdapter.parse(input.value, this.field.dateDisplayFormat);

if (isValid(newValue)) {
this.field.value = DateFnsUtils.utcToLocal(newValue).toISOString();
this.field.value = DateFnsUtils.localToUtc(newValue).toISOString();
} else {
this.field.value = input.value;
}
Expand All @@ -98,7 +98,7 @@ export class DateTimeWidgetComponent extends WidgetComponent implements OnInit {
const input = event.targetElement as HTMLInputElement;

if (newValue && isValid(newValue)) {
this.field.value = DateFnsUtils.utcToLocal(newValue).toISOString();
this.field.value = DateFnsUtils.localToUtc(newValue).toISOString();
} else {
this.field.value = input.value;
}
Expand Down

0 comments on commit 888b5fc

Please sign in to comment.