From 213cf02dab49d6b676bfdd5bc7e4eb1d8d3c9f14 Mon Sep 17 00:00:00 2001 From: Xavier Molloy Date: Fri, 5 Jul 2024 15:03:05 +0200 Subject: [PATCH] chore: [ANDROAPP-6305] show error if date has incorrect date Format both in field and in save pop up (#3717) --- .../org/dhis2/commons/date/DateUtils.java | 1 + .../java/org/dhis2/form/ui/FormViewModel.kt | 21 ++++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/commons/src/main/java/org/dhis2/commons/date/DateUtils.java b/commons/src/main/java/org/dhis2/commons/date/DateUtils.java index 59f19866de..faae1a08ed 100644 --- a/commons/src/main/java/org/dhis2/commons/date/DateUtils.java +++ b/commons/src/main/java/org/dhis2/commons/date/DateUtils.java @@ -27,6 +27,7 @@ public static DateUtils getInstance() { } public static final String DATABASE_FORMAT_EXPRESSION = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"; + public static final String DATABASE_FORMAT_NO_TIME = "yyyy-MM-dd"; public static final String DATABASE_FORMAT_EXPRESSION_NO_MILLIS = "yyyy-MM-dd'T'HH:mm:ss"; public static final String DATABASE_FORMAT_EXPRESSION_NO_SECONDS = "yyyy-MM-dd'T'HH:mm"; public static final String DATE_TIME_FORMAT_EXPRESSION = "yyyy-MM-dd HH:mm"; diff --git a/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt b/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt index 4665261e09..135ba01ff7 100644 --- a/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt +++ b/form/src/main/java/org/dhis2/form/ui/FormViewModel.kt @@ -12,6 +12,7 @@ import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flowOn import kotlinx.coroutines.flow.map import kotlinx.coroutines.launch +import org.dhis2.commons.date.DateUtils import org.dhis2.commons.prefs.PreferenceProvider import org.dhis2.commons.viewmodel.DispatcherProvider import org.dhis2.form.R @@ -39,6 +40,8 @@ import org.hisp.dhis.android.core.common.valuetype.validation.failures.DateFailu import org.hisp.dhis.android.core.common.valuetype.validation.failures.DateTimeFailure import org.hisp.dhis.android.core.common.valuetype.validation.failures.TimeFailure import timber.log.Timber +import java.text.ParseException +import java.text.SimpleDateFormat import java.time.LocalDate import java.time.LocalDateTime import java.time.format.DateTimeFormatter @@ -553,10 +556,10 @@ class FormViewModel( } val formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm") - + val isValidDateFormat = isValidDate(dateTimeString.substring(0, 10)) try { val date = LocalDateTime.parse(dateTimeString, formatter) - if (allowFutureDates == false && date.isAfter(LocalDateTime.now())) { + if (allowFutureDates == false && date.isAfter(LocalDateTime.now()) || !isValidDateFormat) { return Result.Failure(DateFailure.ParseException) } return valueType.validator.validate(dateTimeString) @@ -586,7 +589,8 @@ class FormViewModel( try { val date = LocalDate.parse(dateString, formatter) - if (allowFutureDates == false && date.isAfter(LocalDate.now())) { + val formatDateValid = isValidDate(dateString) + if (allowFutureDates == false && date.isAfter(LocalDate.now()) || !formatDateValid) { return Result.Failure(Throwable()) } return valueType.validator.validate(dateString) @@ -595,6 +599,17 @@ class FormViewModel( } } + private fun isValidDate(text: String): Boolean { + val format = SimpleDateFormat(DateUtils.DATABASE_FORMAT_NO_TIME) + format.isLenient = false + return try { + format.parse(text) + true + } catch (e: ParseException) { + false + } + } + private fun createRowAction( uid: String, value: String?,