Skip to content

Refresh FormField errors after locale changes - #191110

Open
abdullahtas0 wants to merge 2 commits into
flutter:masterfrom
abdullahtas0:agent/refresh-form-errors-on-dependency-change
Open

abdullahtas0 wants to merge 2 commits into
flutter:masterfrom
abdullahtas0:agent/refresh-form-errors-on-dependency-change

Conversation

@abdullahtas0

@abdullahtas0 abdullahtas0 commented Aug 14, 2026 •

Copy link
Copy Markdown

Updates FormFieldState to observe locale changes and rerun the validator only when an enabled field already displays an error. This refreshes localized validation messages without enabling automatic validation for untouched fields.

Fixes #143729.

Tests:

  • ./bin/flutter test packages/flutter/test/widgets/form_test.dart
  • ./bin/flutter analyze --no-pub packages/flutter

Pre-launch Checklist

@github-actions github-actions Bot added the framework flutter/packages/flutter repository. See also f: labels. label Aug 14, 2026
@abdullahtas0
abdullahtas0 marked this pull request as ready for review August 14, 2026 08:56

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Code Review

This pull request updates FormFieldState to re-validate and update its error text when the active locale changes. This is done by tracking the locale in didChangeDependencies and triggering validation if the locale changes while the field is enabled and has an error. Feedback suggests removing the _locale != null check to ensure that restored form states with existing errors are correctly re-validated and translated on the initial build.

Comment on lines +810 to +812
if (_locale != null && locale != _locale && widget.enabled && hasError) {
_validate();
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

By requiring _locale != null, the field will not re-validate on the very first didChangeDependencies call. However, if the form state was restored and already has an error (hasError is true), we want to re-validate on the first build to ensure the restored error message is correctly translated to the current user locale. Removing _locale != null allows the field to correctly refresh its error message to the active locale upon state restoration or when a Localizations widget is dynamically introduced.

    if (locale != _locale && widget.enabled && hasError) {
      _validate();
    }

@justinmc justinmc added the CICD Run CI/CD label Sep 1, 2026
@flutter-dashboard

Copy link
Copy Markdown

This pull request executed golden file tests, but it has not been updated in a while (20+ days). Test results from Gold expire after as many days, so this pull request will need to be updated with a fresh commit in order to get results from Gold.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@abdullahtas0
abdullahtas0 force-pushed the agent/refresh-form-errors-on-dependency-change branch from 65a1d45 to b1fb24e Compare September 25, 2026 13:18
@flutter-dashboard flutter-dashboard Bot removed the CICD Run CI/CD label Sep 25, 2026
@abdullahtas0

Copy link
Copy Markdown
Author

Rebased onto current master and pushed a follow-up so Gold and presubmit can run on a fresh head.

The follow-up addresses the review note about restored errors. _locale != null skipped the first didChangeDependencies call, so an error restored from a previous locale was not revalidated until the locale changed again. Visible errors are now refreshed on that first dependency change as well. Fields that are not already showing an error are still left alone.

Locally: flutter test packages/flutter/test/widgets/form_test.dart --name "localization changes|Restored validator error" — both passed.

The push cleared the CICD label, and I cannot add it back. Could a reviewer re-apply it so presubmit runs on b1fb24e2ee1?

A restored error is already visible before any previous locale is recorded, so the first dependency change has to refresh it too.
@abdullahtas0
abdullahtas0 force-pushed the agent/refresh-form-errors-on-dependency-change branch from b1fb24e to 64b61a2 Compare November 4, 2026 08:58
@abdullahtas0

Copy link
Copy Markdown
Author

Rebased onto current master (3623d2d3454). The September 25 head was past Gold's 20-day retention, so the results need a fresh commit.

./bin/flutter test packages/flutter/test/widgets/form_test.dart — 52 passed, including the locale change and restoration cases.

New head is 64b61a2a006. I can't add the CICD label. Could a reviewer apply it so presubmit runs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

When validatorof TextFormField widget returns an AppLozalizations.of(context).translationKey value, error message does not refresh with locale change

2 participants