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

Highlight form fields that fail validation when submit button is clicked #2722

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
@@ -1,5 +1,5 @@
/*
* Copyright 2023-2024 Google LLC
* Copyright 2023-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 @@ -293,12 +293,6 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat

private lateinit var currentPageItems: List<QuestionnaireAdapterItem>

/**
* True if the user has tapped the next/previous pagination buttons on the current page. This is
* needed to avoid spewing validation errors before any questions are answered.
*/
private var forceValidation = false

/**
* Map of [QuestionnaireResponseItemAnswerComponent] for
* [Questionnaire.QuestionnaireItemComponent]s that are disabled now. The answers will be used to
Expand Down Expand Up @@ -896,7 +890,6 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
val validationResult =
if (
modifiedQuestionnaireResponseItemSet.contains(questionnaireResponseItem) ||
forceValidation ||
isInReviewModeFlow.value
) {
questionnaireResponseItemValidator.validate(
Expand Down Expand Up @@ -1117,13 +1110,14 @@ internal class QuestionnaireViewModel(application: Application, state: SavedStat
it.item.validationResult is NotValidated
}
) {
// Force update validation results for all questions on the current page. This is needed
// when the user has not answered any questions so no validation has been done.
forceValidation = true
// Add all items on the current page to modifiedQuestionnaireResponseItemSet.
// This will ensure that all fields are validated even when they're not filled by the user
currentPageItems.filterIsInstance<QuestionnaireAdapterItem.Question>().forEach {
modifiedQuestionnaireResponseItemSet.add(it.item.getQuestionnaireResponseItem())
}
// Results in a new questionnaire state being generated synchronously, i.e., the current
// thread will be suspended until the new state is generated.
modificationCount.update { it + 1 }
forceValidation = false
}

if (
Expand Down
Loading