Repository navigation
AutovalidateMode.always on Form not working as expected #142701
Description
Steps to reproduce
Display the following widget:
Form(
autovalidateMode: AutovalidateMode.always,
child: TextFormField(
validator: (value) => value?.isEmpty == true ? 'Empty' : null,
),
),
Expected results
We would expect the error message "Empty", to be displayed under the TextFormField, because the Form has AutovalidateMode.always and the value of the field is empty.
Actual results
The error message is not displayed when the Form is rendered. Only when a change happens on the TextFormField, for example adding and removing text, the error is displayed.
Workaround
When the TextFormField has AutovalidateMode.always, the validation does happen immediately and the error message is displayed.
Looking into the source code, Form runs the _validate function in the beginning of its build method, before its _fields have registered. The _validate method goes over an empty Set of fields.
The FormField on the other hand sets its own _errorText before calling the builder is called, causing the actual builder of TextFormField to have the correct errorText available at the first time it builds.
Code sample
Code sample
import 'package:flutter/material.dart';
void main() {
runApp(const MainApp());
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: Center(
child: Form(
autovalidateMode: AutovalidateMode.always,
child: TextFormField(
validator: (value) => value?.isEmpty == true ? 'Empty' : null,
),
),
),
),
);
}
}Screenshots or Video
Screenshots / Video demonstration
[Upload media here]
Logs
Logs
[Paste your logs here]Flutter Doctor output
Doctor output
[✓] Flutter (Channel stable, 3.16.9, on macOS 14.2.1 23C71 darwin-arm64, locale en-BE)
• Flutter version 3.16.9 on channel stable at /Users/fre/fvm/versions/stable
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 41456452f2 (7 days ago), 2024-01-25 10:06:23 -0800
• Engine revision f40e976bed
• Dart version 3.2.6
• DevTools version 2.28.5
[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
• Android SDK at /Users/fre/Library/Android/sdk
• Platform android-34, build-tools 34.0.0
• ANDROID_HOME = /Users/fre/Library/Android/sdk
• Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 15.1)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Build 15C65
• CocoaPods version 1.14.3
[✓] Chrome - develop for the web
• Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
[✓] Android Studio (version 2023.1)
• Android Studio at /Applications/Android Studio.app/Contents
• Flutter plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
🔨 https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
[✓] IntelliJ IDEA Community Edition (version 2023.3.2)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
• Flutter plugin version 77.0.1
• Dart plugin version 233.13135.65
[✓] VS Code (version 1.85.2)
• VS Code at /Applications/Visual Studio Code.app/Contents
• Flutter extension version 3.80.0
[✓] Connected device (3 available)
• iPhone SE (mobile) • 00008110-000E7901349B801E • ios • iOS 17.2.1 21C66
• macOS (desktop) • macos • darwin-arm64 • macOS 14.2.1 23C71 darwin-arm64
• Chrome (web) • chrome • web-javascript • Google Chrome 121.0.6167.139
[✓] Network resources
• All expected network resources are available.
• No issues found!
Activity
dumazy commentedon Feb 1, 2024
I found there's also an invalid test that forces another rebuild to get a positive result:
flutter/packages/flutter/test/widgets/form_test.dart
Line 889 in e02e207
dam-ease commentedon Feb 1, 2024
Thanks for the report @dumazy.
I can reproduce this on the latest
masterandstablechannels.From docs,
On opening of app, the error isn't displayed since TextField is empty.
stable, master flutter doctor -v
neoacevedo commentedon Aug 5, 2024
I'm using Flutter 3.19.6. I know this issue has 6 months old.
If is needed, I will open a new issue, but, the validator for each field works, marking them in red and displaying the error validation as in the picture:
But as you can see in the video, using autovalidatemode in always, the keyboard shows/hides and is impossible to type anything:
flutter_keyboard.webm
Using autovalidatemode in onUserInteraction doesn't have such performance, but changing the focus of each field, the validation won't work:
flutter_keyboard_1.webm
EDIT: once the validation start on typing anything, the performance is exactly the same as the seen in the first video.
Fixes initial validation with AutovalidateMode.always on first build (#…
github-actions commentedon Dec 3, 2024
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.