Skip to content

Commit 36e2ccf

Browse files
Highlight form fields that fail validation when submit button is clicked (#2722)
1 parent b483ed0 commit 36e2ccf

File tree

1 file changed

+6
-12
lines changed

1 file changed

+6
-12
lines changed

datacapture/src/main/java/com/google/android/fhir/datacapture/QuestionnaireViewModel.kt

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2023-2024 Google LLC
2+
* Copyright 2023-2025 Google LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -298,12 +298,6 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
298298

299299
private lateinit var currentPageItems: List<QuestionnaireAdapterItem>
300300

301-
/**
302-
* True if the user has tapped the next/previous pagination buttons on the current page. This is
303-
* needed to avoid spewing validation errors before any questions are answered.
304-
*/
305-
private var forceValidation = false
306-
307301
/**
308302
* Map of [QuestionnaireResponseItemAnswerComponent] for
309303
* [Questionnaire.QuestionnaireItemComponent]s that are disabled now. The answers will be used to
@@ -903,7 +897,6 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
903897
val validationResult =
904898
if (
905899
modifiedQuestionnaireResponseItemSet.contains(questionnaireResponseItem) ||
906-
forceValidation ||
907900
isInReviewModeFlow.value
908901
) {
909902
questionnaireResponseItemValidator.validate(
@@ -1124,13 +1117,14 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
11241117
it.item.validationResult is NotValidated
11251118
}
11261119
) {
1127-
// Force update validation results for all questions on the current page. This is needed
1128-
// when the user has not answered any questions so no validation has been done.
1129-
forceValidation = true
1120+
// Add all items on the current page to modifiedQuestionnaireResponseItemSet.
1121+
// This will ensure that all fields are validated even when they're not filled by the user
1122+
currentPageItems.filterIsInstance<QuestionnaireAdapterItem.Question>().forEach {
1123+
modifiedQuestionnaireResponseItemSet.add(it.item.getQuestionnaireResponseItem())
1124+
}
11301125
// Results in a new questionnaire state being generated synchronously, i.e., the current
11311126
// thread will be suspended until the new state is generated.
11321127
modificationCount.update { it + 1 }
1133-
forceValidation = false
11341128
}
11351129

11361130
if (

0 commit comments

Comments
 (0)