Skip to content

AutovalidateMode.always on Form not working as expected #142701

Closed

Description

@dumazy

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

  1. dumazy commented on Feb 1, 2024

    @dumazy
    ContributorAuthor

    I found there's also an invalid test that forces another rebuild to get a positive result:

    await tester.pumpWidget(builder());

    // The issue only happens on the second build so we
    // need to rebuild the tree twice.
    await tester.pumpWidget(builder());
    await tester.pumpWidget(builder());
  2. added
    in triagePresently being triaged by the triage team
    on Feb 1, 2024
  3. dam-ease commented on Feb 1, 2024

    @dam-ease

    Thanks for the report @dumazy.
    I can reproduce this on the latest master and stable channels.
    From docs,

    Used to auto-validate [Form] and [FormField] even without user interaction.

    On opening of app, the error isn't displayed since TextField is empty.

    stable, master flutter doctor -v

    [!] Flutter (Channel stable, 3.16.9, on macOS 14.2.1 23C71 darwin-arm64, locale
        en-NG)
        • Flutter version 3.16.9 on channel stable at
          /Users/damilolaalimi/sdks/flutter
        ! Warning: `dart` on your path resolves to
          /opt/homebrew/Cellar/dart/3.1.5/libexec/bin/dart, which is not inside your
          current Flutter SDK checkout at /Users/damilolaalimi/sdks/flutter.
          Consider adding /Users/damilolaalimi/sdks/flutter/bin to the front of your
          path.
        • 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
        • If those were intentional, you can disregard the above warnings; however
          it is recommended to use "git" directly to perform update checks and
          upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
        • Android SDK at /Users/damilolaalimi/Library/Android/sdk
        • Platform android-34, build-tools 34.0.0
        • ANDROID_HOME = /Users/damilolaalimi/Library/Android/sdk
        • Java binary at: /Applications/Android
          Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build
          17.0.6+0-17.0.6b802.4-9586694)
        • 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 2022.2)
        • 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.6+0-17.0.6b802.4-9586694)
    
    [!] Android Studio (version unknown)
        • Android Studio at /Users/damilolaalimi/Downloads/Android Studio
          Preview.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
        ✗ Unable to determine Android Studio version.
        • Java version OpenJDK Runtime Environment (build
          17.0.7+0-17.0.7b1000.6-10550314)
    
    [✓] VS Code (version 1.85.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] VS Code (version 1.83.1)
        • VS Code at /Users/damilolaalimi/Downloads/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] Connected device (4 available)
        • Damilola’s iPhone (mobile) • 00008110-001964480AE1801E            • ios
          • iOS 17.1.1 21B91
        • iPhone 15 Pro (mobile)     • 79E44FCE-BEA4-4C4B-9C1C-EFBD9964E172 • ios
          • com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator)
        • macOS (desktop)            • macos                                •
          darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
        • Chrome (web)               • chrome                               •
          web-javascript • Google Chrome 121.0.6167.85
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 2 categories.
    
    [!] Flutter (Channel master, 3.19.0-15.0.pre.38, on macOS 14.2.1 23C71 darwin-arm64, locale en-NG)
        • Flutter version 3.19.0-15.0.pre.38 on channel master at /Users/damilolaalimi/fvm/versions/master
        ! Warning: `dart` on your path resolves to /opt/homebrew/Cellar/dart/3.1.5/libexec/bin/dart, which is not inside your current Flutter SDK checkout at /Users/damilolaalimi/fvm/versions/master. Consider adding /Users/damilolaalimi/fvm/versions/master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision e02e2079be (4 hours ago), 2024-02-01 02:00:32 -0500
        • Engine revision 68943afd62
        • Dart version 3.4.0 (build 3.4.0-97.0.dev)
        • DevTools version 2.31.0
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
        • Android SDK at /Users/damilolaalimi/Library/Android/sdk
        • Platform android-34, build-tools 34.0.0
        • ANDROID_HOME = /Users/damilolaalimi/Library/Android/sdk
        • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
        • 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 2022.2)
        • 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.6+0-17.0.6b802.4-9586694)
    
    [!] Android Studio (version unknown)
        • Android Studio at /Users/damilolaalimi/Downloads/Android Studio Preview.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
        ✗ Unable to determine Android Studio version.
        • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    
    [✓] VS Code (version 1.85.2)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] VS Code (version 1.83.1)
        • VS Code at /Users/damilolaalimi/Downloads/Visual Studio Code.app/Contents
        • Flutter extension version 3.78.0
    
    [✓] Connected device (5 available)
        • Damilola’s iPhone (mobile)      • 00008110-001964480AE1801E            • ios            • iOS 17.1.1 21B91
        • iPhone 15 Pro (mobile)          • 79E44FCE-BEA4-4C4B-9C1C-EFBD9964E172 • ios            • com.apple.CoreSimulator.SimRuntime.iOS-17-2 (simulator)
        • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 14.2.1 23C71 darwin-arm64
        • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 14.2.1 23C71 darwin-arm64
        • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 121.0.6167.85
    
    [✓] Network resources
        • All expected network resources are available.
    
    ! Doctor found issues in 2 categories.
    

  4. added
    a: text inputEntering text in a text field or keyboard related problems
    frameworkflutter/packages/flutter repository. See also f: labels.
    p: material_uimaterial_ui package in flutter/packages
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    team-designOwned by Design Languages team
    and removed
    in triagePresently being triaged by the triage team
    on Feb 1, 2024
  5. added
    P2Important issues not at the top of the work list
    triaged-designTriaged by Design Languages team
    on Feb 1, 2024
  6. neoacevedo commented on Aug 5, 2024

    @neoacevedo

    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.

  7. added and removed
    team-designOwned by Design Languages team
    triaged-designTriaged by Design Languages team
    on Oct 17, 2024
  8. added a commit that references this issue on Nov 19, 2024
    fc53c71
  9. github-actions commented on Dec 3, 2024

    @github-actions

    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 -v and a minimal reproduction of the issue.

  10. locked as resolved and limited conversation to collaborators on Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsfound in release: 3.16Found to occur in 3.16found in release: 3.19Found to occur in 3.19frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: material_uimaterial_ui package in flutter/packagesteam-text-inputOwned by Text Input teamtriaged-text-inputTriaged by Text Input team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions