Skip to content

Commit

Permalink
chore: [ANDROAPP-6305] show error if date has incorrect date Format b…
Browse files Browse the repository at this point in the history
…oth in field and in save pop up (#3717)
  • Loading branch information
xavimolloy authored Jul 5, 2024
1 parent d5a931b commit 213cf02
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
21 changes: 18 additions & 3 deletions form/src/main/java/org/dhis2/form/ui/FormViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand All @@ -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?,
Expand Down

0 comments on commit 213cf02

Please sign in to comment.