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

Include min and max values in the validation message in a number question if the values are in the extension #2763

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022-2024 Google LLC
* Copyright 2022-2025 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -38,6 +38,18 @@ internal object EditTextIntegerViewHolderFactory :
QuestionnaireItemEditTextViewHolderDelegate(
InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_SIGNED,
) {

override fun bind(questionnaireViewItem: QuestionnaireViewItem) {
super.bind(questionnaireViewItem)

val minValue = (questionnaireViewItem.minAnswerValue as? IntegerType)?.value
val maxValue = (questionnaireViewItem.maxAnswerValue as? IntegerType)?.value

if (minValue != null && maxValue != null && minValue > maxValue) {
throw IllegalArgumentException("minValue cannot be greater than maxValue")
}
}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel overriding the bind function is not necessary, updateValidationTextUI function is already called in the parent's bind (you can check it), so throwing the error in updateValidationTextUI should suffice.

What do you think?

Copy link
Contributor Author

@chungwwei chungwwei Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had it that way initially, but i thought placing the constraint check didn’t fit the intent of the function name updateValidationTextUI, so I moved it to bind. Like jingtang10 commented, it would be better to have it in the view model to remove redundancy

maybe have it in QuestionnaireItemViewHolder, I believe all the factories are extending QuestionnaireItemViewHolder

fun bind(questionnaireViewItem: QuestionnaireViewItem) {
delegate.questionnaireViewItem = questionnaireViewItem
delegate.bind(questionnaireViewItem)
itemMediaView.bind(questionnaireViewItem.questionnaireItem)
delegate.setReadOnly(questionnaireViewItem.questionnaireItem.readOnly)
}
}

override suspend fun handleInput(
editable: Editable,
questionnaireViewItem: QuestionnaireViewItem,
Expand Down Expand Up @@ -90,13 +102,19 @@ internal object EditTextIntegerViewHolderFactory :
questionnaireViewItem,
questionnaireViewItem.validationResult,
)

val minValue =
(questionnaireViewItem.minAnswerValue as? IntegerType)?.value ?: Int.MIN_VALUE
val maxValue =
(questionnaireViewItem.maxAnswerValue as? IntegerType)?.value ?: Int.MAX_VALUE

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens when the minValue is bigger than the maxValue?
how about we throw an exception when that happens?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added the exception. UI will crash when minValue > maxValue, like the slider and date widgets.

Copy link
Collaborator

@FikriMilano FikriMilano Jan 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, but feel free to speak your thoughts on this, we could for example: show an error message to the user, instead of outright crashing the app. We're open for discussion.

What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ig it's rare for a scenario like that to happen. Adheres to the rules, but with nonsensical values.

Alternatively, I view the minValue > maxValue a meaningless constraint on the UI, so it make sense to treat it as though the constraint doesn’t exist? We can set minAnswerValue and maxAnswerValue to null if when that happens.

Looks like the web app https://lhcforms.nlm.nih.gov/lhcforms ignored that scenario

// Update error message if draft answer present
if (questionnaireViewItem.draftAnswer != null) {
textInputLayout.error =
textInputLayout.context.getString(
R.string.integer_format_validation_error_msg,
formatInteger(Int.MIN_VALUE),
formatInteger(Int.MAX_VALUE),
formatInteger(minValue),
formatInteger(maxValue),
)
}
}
Expand Down