Skip to content

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.

_formState?._fieldDidChange();
}

bool get _shouldValidate {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a nice way to encapsulate this important logic 👍

/// [FormState.validateGranularly].
manual;

bool get isAuto => this == _FormValidation.auto;
Copy link
Contributor

@justinmc justinmc Jan 16, 2026

Choose a reason for hiding this comment

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

Is this necessary? Is it just to save a few lines?

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