Skip to content

Form fields in autovalidate form are validated on the second build (but not the first) #15404

Closed

Description

@Hixie

If you have a TextFormField with a validator in a Form with autovalidate set to true, the validator is not evaluated when the form field is first created, but it is evaluated the next time the Form builds, even if that happens for unrelated reasons (e.g. an animation, or a reassemble during hot reload). So for example this means that hot-reloading an auto-validate form will make all the validators trigger and all the text fields turn red.

cc @HansMuller

Activity

  1. 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
    on Mar 11, 2018
  2. added this to the milestone on Mar 11, 2018
  3. changed the title [-]TextFormField shows error text when it rebuilds[/-] [+]Form fields in autovalidate form are validated on the second build (but not the first)[/+] on Mar 11, 2018
  4. Hixie commented on Mar 12, 2018

    @Hixie
    ContributorAuthor

    I think we should reimplement autovalidate by having the build methods of fields check if it's set and if so validate themselves, rather than having the form do it.

  5. meggamind commented on Apr 6, 2018

    @meggamind

    @Hixie : I would like to take this issue. I will try to reproduce this issue and get back to you. Is there a demo app that already shows this issue ?

  6. HansMuller commented on Apr 6, 2018

    @HansMuller
    Contributor

    I think you'll be able to reproduce the problem with the gallery TextField demo, https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart

    Thanks for taking this on!

  7. zoechi commented on Jan 16, 2019

    @zoechi
    Contributor

    I wasn't able to reproduce with Gallery.
    A hot-reload doesn't trigger validation, only clicking the [SUBMIT] button.

    [✓] Flutter (Channel dev, v1.1.9, on Mac OS X 10.14.1 18B75, locale en-AT)
    

    I don't close because I'm not 100% sure I understand what the issue is.

  8. li2 commented on Feb 10, 2019

    @li2

    _autovalidate won't work without setState, see attached image,

    line 107 https://github.com/flutter/flutter/blob/master/examples/flutter_gallery/lib/demo/material/text_form_field_demo.dart

    -      _autovalidate = true; // Start validating on every change.
    +      setState(() {
    +        _autovalidate = true; // Start validating on every change.
    +      });
    

    image

  9. jeisux92 commented on Apr 18, 2020

    @jeisux92

    if the Form Widget is set to autovalidate the validator is exectuted at first time, I think it's a bug

  10. pedromassango commented on May 5, 2020

    @pedromassango
    Member

    @jeisux92 did you found a workaround for this bug?

  11. pedromassango commented on May 5, 2020

    @pedromassango
    Member

    Why TextFormField is auto-validating itself without updating its content? I have autovalidate set to true, but is this expect to happen?
    Note: this is causing my all my fields red without updating its content.

    @Hixie is this a expected behaviour?

  12. slightfoot commented on May 5, 2020

    @slightfoot
    Member

    I have found this many times. I see it has a design-flaw/bug. Their is no way anyone would want an entire form to autovalidate and show errors as soon as it is displayed.

  13. added
    P2Important issues not at the top of the work list
    on May 29, 2020
  14. SaulPT commented on Jun 2, 2020

    @SaulPT

    Yes i agree. I think autovalidate should only run when a field value changes.
    I just did a workaround, using GlobalKey<FormFieldState> and I just run the validation on the onChanged property:

    final GlobalKey<FormFieldState> formFieldKey = GlobalKey<FormFieldState>();
    TextFormField(
      key: formFieldKey,
      validator: (value) {
        if (value.trim().length == 0) {
          return 'Value is required!';
        }
        return null;
      },
      onChanged: (value) {
        formFieldKey.currentState.validate();
      },
    )
  15. pedromassangocode commented on Jul 16, 2020

    @pedromassangocode

    This is reproducible in 1.20.0-7.1.pre.

    Complete sample code
    import 'package:flutter/material.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(
              title: Text('App Name'),
            ),
            body: Center(
              child: Form(
                autovalidate: true,
                child: TextFormField(
                  validator: (e) => 'error/$e',
                ),
              ),
            ),
          ),
        );
      }
    }
    flutter doctor -v
    [✓] Flutter (Channel dev, 1.20.0-7.1.pre, on Mac OS X 10.15.5 19F101, locale en-AO)
        • Flutter version 1.20.0-7.1.pre at /Users/pedro/dev/SDKs/flutter_dev
        • Framework revision 7736f3bc90 (6 days ago), 2020-07-10 16:33:05 -0700
        • Engine revision d48085141c
        • Dart version 2.9.0 (build 2.9.0-21.2.beta)
    
     
    [✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
        • Android SDK at /Users/pedro/Library/Android/sdk
        • Platform android-30, build-tools 30.0.0
        • Java binary at: /Users/pedro/Library/Application
          Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6514223/Android
          Studio.app/Contents/jre/jdk/Contents/Home/bin/java
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 11.5)
        • Xcode at /Applications/Xcode.app/Contents/Developer
        • Xcode 11.5, Build version 11E608c
        • CocoaPods version 1.9.3
    
    [✓] Chrome - develop for the web
        • CHROME_EXECUTABLE = /Applications/Google
          Chrome.app/Contents/MacOS/google-chrome-unsafe
    
    [✓] Android Studio (version 4.0)
        • Android Studio at /Applications/Android Studio.app/Contents
        • Flutter plugin version 47.1.2
        • Dart plugin version 193.7361
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    
    [✓] Android Studio (version 4.0)
        • Android Studio at /Users/pedro/Library/Application
          Support/JetBrains/Toolbox/apps/AndroidStudio/ch-0/193.6514223/Android
          Studio.app/Contents
        • Flutter plugin version 47.1.2
        • Dart plugin version 193.7361
        • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6222593)
    
    [✓] VS Code (version 1.47.0)
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.12.1
    
    [✓] Connected device (4 available)
        • AOSP on IA Emulator (mobile) • emulator-5554 • android-x86    • Android 9 (API 28)
          (emulator)
        • macOS (desktop)              • macos         • darwin-x64     • Mac OS X 10.15.5
          19F101
        • Web Server (web)             • web-server    • web-javascript • Flutter Tools
        • Chrome (web)                 • chrome        • web-javascript • Google Chrome
          83.0.4103.116
    
    • No issues found!
    
  16. github-actions commented on Aug 15, 2021

    @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.

  17. locked as resolved and limited conversation to collaborators on Aug 15, 2021
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: 1.17Found to occur in 1.17found in release: 1.20Found to occur in 1.20frameworkflutter/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/packages

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions