From 2402f1b019b14cd3fa74fa18df4e8d8e1be43e74 Mon Sep 17 00:00:00 2001 From: Tony Valle Date: Tue, 1 Oct 2024 10:09:12 +0200 Subject: [PATCH 1/2] fix: check return value of `parseDate` --- src/core_modules/capture-core/converters/formToClient.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/core_modules/capture-core/converters/formToClient.js b/src/core_modules/capture-core/converters/formToClient.js index 7134f1881c..1e8a933f36 100644 --- a/src/core_modules/capture-core/converters/formToClient.js +++ b/src/core_modules/capture-core/converters/formToClient.js @@ -30,8 +30,9 @@ function convertDateTime(formValue: DateTimeValue): string { } function convertDate(dateValue: string) { + const parsedDate = parseDate(dateValue); // $FlowFixMe[incompatible-use] automated comment - return parseDate(dateValue).momentDate.toISOString(); + return parsedDate.isValid ? parsedDate.momentDate.toISOString() : null; } function convertTime(timeValue: string) { From ea58565227f51f3732b39bec0a40910ea54d4d18 Mon Sep 17 00:00:00 2001 From: Tony Valle Date: Thu, 3 Oct 2024 13:48:35 +0200 Subject: [PATCH 2/2] fix: validation of Time and DateTime fields in TE registration page --- .../capture-core/converters/formToClient.js | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/core_modules/capture-core/converters/formToClient.js b/src/core_modules/capture-core/converters/formToClient.js index 1e8a933f36..adca6b5b72 100644 --- a/src/core_modules/capture-core/converters/formToClient.js +++ b/src/core_modules/capture-core/converters/formToClient.js @@ -14,16 +14,20 @@ type RangeValue = { to: string, } -function convertDateTime(formValue: DateTimeValue): string { +function convertDateTime(formValue: DateTimeValue): ?string { const editedDate = formValue.date; const editedTime = formValue.time; - const momentTime = parseTime(editedTime).momentTime; + const parsedTime = parseTime(editedTime); + if (!parsedTime.isValid) return null; + const momentTime = parsedTime.momentTime; const hours = momentTime.hour(); const minutes = momentTime.minute(); + const parsedDate = parseDate(editedDate); + if (!parsedDate.isValid) return null; // $FlowFixMe[incompatible-type] automated comment - const momentDateTime: moment$Moment = parseDate(editedDate).momentDate; + const momentDateTime: moment$Moment = parsedDate.momentDate; momentDateTime.hour(hours); momentDateTime.minute(minutes); return momentDateTime.toISOString(); @@ -36,7 +40,9 @@ function convertDate(dateValue: string) { } function convertTime(timeValue: string) { - const momentTime = parseTime(timeValue).momentTime; + const parsedTime = parseTime(timeValue); + if (!parsedTime.isValid) return null; + const momentTime = parsedTime.momentTime; momentTime.locale('en'); return momentTime.format('HH:mm'); }