Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: [ANDROAPP-6104] do not allow future dates in date fields #3710

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ private void initSearchParameters() {
uid,
selectedOrgUnit,
ValueType.ORGANISATION_UNIT,
null
null,
true
)
);
return Unit.INSTANCE;
Expand Down
3 changes: 2 additions & 1 deletion form/src/main/java/org/dhis2/form/ui/FormViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,7 @@ class FormViewModel(
intent.valueType,
intent.value,
intent.fieldMask,
intent.allowFutureDates,
)

createRowAction(
Expand Down Expand Up @@ -586,7 +587,7 @@ class FormViewModel(
try {
val date = LocalDate.parse(dateString, formatter)
if (allowFutureDates == false && date.isAfter(LocalDate.now())) {
return Result.Failure(DateFailure.ParseException)
return Result.Failure(Throwable())
}
return valueType.validator.validate(dateString)
} catch (e: DateTimeParseException) {
Expand Down
2 changes: 1 addition & 1 deletion form/src/main/java/org/dhis2/form/ui/intent/FormIntent.kt
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ sealed class FormIntent {
val value: String?,
val valueType: ValueType?,
val fieldMask: String? = null,
val allowFutureDates: Boolean? = false,
) : FormIntent()

data class OnQrCodeScanned(
Expand All @@ -42,7 +43,6 @@ sealed class FormIntent {
val uid: String,
val value: String?,
val valueType: ValueType?,
val allowFutureDates: Boolean? = false,
) : FormIntent()

data class ClearValue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,17 @@ fun ProvideInputAge(
}

is AgeInputType.DateOfBirth -> {
saveValue(
intentHandler,
fieldUiModel.uid,
formatUIDateToStored(type.value.text),
fieldUiModel.valueType,
fieldUiModel.allowFutureDates,
)
formatUIDateToStored(type.value.text)
.takeIf { it != fieldUiModel.value }
?.let {
saveValue(
intentHandler,
fieldUiModel.uid,
it,
fieldUiModel.valueType,
fieldUiModel.allowFutureDates,
)
}
}

AgeInputType.None -> {
Expand All @@ -135,14 +139,23 @@ private fun saveValue(
valueType: ValueType?,
allowFutureDates: Boolean?,
) {
intentHandler.invoke(
FormIntent.OnTextChange(
uid,
value,
valueType,
allowFutureDates ?: false,
),
)
when (value?.length) {
null, 10 -> intentHandler.invoke(
FormIntent.OnSave(
uid,
value,
valueType,
allowFutureDates = allowFutureDates,
),
)
else -> intentHandler.invoke(
FormIntent.OnTextChange(
uid,
value,
valueType,
),
)
}
}

private fun formatStoredDateToUI(inputDateString: String): String {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,21 @@ fun ProvideInputDate(
onNextClicked = onNextClicked,
onValueChanged = {
value = it ?: TextFieldValue()
intentHandler.invoke(
val formIntent = if (value.text.length == 8) {
FormIntent.OnSave(
uid = fieldUiModel.uid,
value = formatUIDateToStored(it?.text, fieldUiModel.valueType),
valueType = fieldUiModel.valueType,
allowFutureDates = fieldUiModel.allowFutureDates,
)
} else {
FormIntent.OnTextChange(
uid = fieldUiModel.uid,
value = formatUIDateToStored(it?.text, fieldUiModel.valueType),
valueType = fieldUiModel.valueType,
allowFutureDates = fieldUiModel.allowFutureDates ?: true,
),
)
)
}
intentHandler.invoke(formIntent)
},
selectableDates = selectableDates,
yearRange = yearIntRange,
Expand Down
Loading