Skip to content

Add useStrictAutovalidateMode to respect per-field autovalidateMode#180822

Closed
Mairramer wants to merge 18 commits intoflutter:masterfrom
Mairramer:form/use-strict-autovalidate-mode
Closed

Add useStrictAutovalidateMode to respect per-field autovalidateMode#180822
Mairramer wants to merge 18 commits intoflutter:masterfrom
Mairramer:form/use-strict-autovalidate-mode

Conversation

@Mairramer
Copy link
Contributor

This PR introduces the useStrictAutovalidateMode property to the Form widget. When enabled, each descendant FormField respects its own autovalidateMode, allowing fields to opt out of automatic validation even if the parent Form has a global AutovalidateMode set.

Fixes #125766

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@github-actions github-actions bot added the framework flutter/packages/flutter repository. See also f: labels. label Jan 11, 2026
@Mairramer Mairramer marked this pull request as ready for review January 12, 2026 16:28
@Mairramer
Copy link
Contributor Author

/gemini review

Copy link
Contributor

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

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 introduces the useStrictAutovalidateMode property to the Form widget, providing more granular control over field validation. The implementation is well-structured, particularly the refactoring of autovalidation logic into the _shouldAutoValidate getter, which improves readability. However, I've identified a significant issue with the caching of FormState within FormFieldState that could lead to incorrect behavior and memory leaks when the widget tree changes dynamically. I've provided a detailed comment and a code suggestion to address this. Additionally, there's a minor cleanup opportunity in one of the new tests. Overall, this is a valuable feature, and with the suggested fix, it will be a solid contribution.

@justinmc justinmc self-requested a review January 15, 2026 00:13
Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

I think validation should have been handled programmatically instead of via an enum, but that's a major change that will have to come in another iteration of Form. For now I agree that this is a problem we should solve along these lines. See my comment asking about using the null value of autovalidateMode.

Comment on lines +236 to +249
/// Whether descendant [FormField]s should ignore the [Form.autovalidateMode]
/// and strictly use their own [FormField.autovalidateMode].
///
/// When true, each [FormField] strictly follows its own
/// [FormField.autovalidateMode], ignoring the [Form]'s setting.
///
/// When false, the [Form]'s [AutovalidateMode] takes precedence and determines
/// the automatic validation behavior of all descendant fields.
///
/// Calling [FormState.validate] always triggers validation for all fields,
/// regardless of this setting.
///
/// Defaults to false.
final bool useStrictAutovalidateMode;
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this be done by seeing if autovalidateMode is null, or why wouldn't that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Regarding the null value of autovalidateMode, to be honest, I didn’t give it much thought. I used useStrictAutovalidateMode to make the validation behavior explicit: it ensures that each FormField either follows its own autovalidateMode or inherits the Form’s, depending on what I want.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

One thing I’ve been thinking about is moving this to the FormField level instead of the Form. That might make it easier to manage which fields should not be validated. I’m not sure why someone would want that, but it could still be a good option, especially since we currently don’t have programmatic validation.

Copy link
Contributor

Choose a reason for hiding this comment

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

Would something like this give you enough control and be doable?

  • When FormField.autovalidateMode is non-null
    • Always use it and ignore Form.autovalidateMode.
  • When FormField.autovalidateMode is null
    • Defer to Form.autovalidateMode.
    • If Form.autovalidateMode is also null, default to .disabled.

Or maybe that's a potential breaking change for existing users?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In my opinion, since both currently default to AutovalidateMode.disabled, and because null is effectively treated the same as AutovalidateMode.disabled, changing this behavior would indeed be a breaking change.

Introducing useStrictAutovalidateMode addresses this by preserving existing behavior and avoiding regressions. If we were to adopt the alternative approach you suggested, it would result in a breaking change, albeit one that could be justified as correcting an unintuitive or inconsistent behavior rather than introducing a new feature.

Copy link
Contributor

Choose a reason for hiding this comment

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

@Mairramer Are you currently facing the problem described in #125766 in your own work? If so could you describe what you want to achieve there and how you expected autovalidateMode to work? Just trying to make sure I'm considering what devs want here.

Current behavior: The value on Form takes precedence, except if it's disabled (or explicitly null).

My interpretation of #125766 is that users find the current behavior unintuitive (I do too). They expect FormField.autovalidateMode to override Form.autovalidateMode. In the long run, that's the direction that I think Flutter should go, but as I've said elsewhere, Form needs a rewrite and maybe this isn't the time to make a big breaking change.

So that leaves us with the option of introducing a sub-optimal new API for the short term. My question is, does that solve a significant user problem today that makes it worth adding the complexity?

Copy link
Contributor Author

@Mairramer Mairramer Feb 21, 2026

Choose a reason for hiding this comment

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

@Mairramer Are you currently facing the problem described in #125766 in your own work? If so could you describe what you want to achieve there and how you expected autovalidateMode to work? Just trying to make sure I'm considering what devs want here.

At the moment, this isn’t a major issue for me. Since I work at a company with a design system, it helps mitigate these problems to some extent. However, introducing this would still benefit many of the wrappers we currently maintain.

Current behavior: The value on Form takes precedence, except if it's disabled (or explicitly null).
My interpretation of #125766 is that users find the current behavior unintuitive (I do too). They expect FormField.autovalidateMode to override Form.autovalidateMode. In the long run, that's the direction that I think Flutter should go, but as I've said elsewhere, Form needs a rewrite and maybe this isn't the time to make a big breaking change.

So that leaves us with the option of introducing a sub-optimal new API for the short term. My question is, does that solve a significant user problem today that makes it worth adding the complexity?

I implemented this following your suggestion #180822 (comment). However, it introduces a breaking change: widget.validator cannot call setState. Please see this commit for context: 1182874, similar to this a3d0181 .

I’d like to confirm the best way to avoid this breaking change during the first build.

My proposal to introduce useStrictAutovalidateMode is intended to preserve the current behavior without introducing breaking changes at this time.

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

I want to be sure of the useStrictAutovalidateMode API before we add it, see my comment.

Comment on lines +236 to +249
/// Whether descendant [FormField]s should ignore the [Form.autovalidateMode]
/// and strictly use their own [FormField.autovalidateMode].
///
/// When true, each [FormField] strictly follows its own
/// [FormField.autovalidateMode], ignoring the [Form]'s setting.
///
/// When false, the [Form]'s [AutovalidateMode] takes precedence and determines
/// the automatic validation behavior of all descendant fields.
///
/// Calling [FormState.validate] always triggers validation for all fields,
/// regardless of this setting.
///
/// Defaults to false.
final bool useStrictAutovalidateMode;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would something like this give you enough control and be doable?

  • When FormField.autovalidateMode is non-null
    • Always use it and ignore Form.autovalidateMode.
  • When FormField.autovalidateMode is null
    • Defer to Form.autovalidateMode.
    • If Form.autovalidateMode is also null, default to .disabled.

Or maybe that's a potential breaking change for existing users?

@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. labels Feb 21, 2026
@github-actions github-actions bot removed a: text input Entering text in a text field or keyboard related problems f: material design flutter/packages/flutter/material repository. labels Feb 21, 2026
@Mairramer
Copy link
Contributor Author

I’ve submitted a new PR following the expected pattern #182752.

@Mairramer
Copy link
Contributor Author

Closing this in favor of #182752

@Mairramer Mairramer closed this Feb 24, 2026
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.

TextFormField do not disabled validation when set AutovalidateMode.disabled and Form parent has autovalidateMode != AutovalidateMode.disabled

2 participants