Skip to content

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

@deandreamatias

Description

@deandreamatias

Is there an existing issue for this?

Steps to reproduce

  1. Run code sample
  2. Write text into TextFormField
  3. Error: Show error even autovalidateMode on TextFormField is disabled

Expected results

When TextFormField has the property autovalidateMode with value AutovalidateMode.disabled, the field won't show the error, event with property autovalidateMode with AutovalidateMode.always or AutovalidateMode.onUserInteraction on Form parent widget

This would be follow the Flutter's inherited widget convention were the innermost takes priority (#107350 (comment))

Actual results

When Form parent widget has property autovalidateMode with AutovalidateMode.always or AutovalidateMode.onUserInteraction and TextFormField has the property autovalidateMode with value AutovalidateMode.disabled; the TextFormField validate after the first user validation, like AutovalidateMode.onUserInteraction

Code sample

Code sample
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Form(
                autovalidateMode: AutovalidateMode.always,
                child: TextFormField(
                  autovalidateMode: AutovalidateMode.disabled,
                  validator: (value) =>
                      value == null || value.length < 4 ? 'Not valid' : null,
                ),
              ),
            ],
          ),
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => _formKey.currentState!.validate(),
        tooltip: 'Validate',
        child: const Icon(Icons.check),
      ),
    );
  }
}

Screenshots or Video

Screenshots / Video demonstration
FORM-ISSUE.webm

Logs

Logs
[Paste your logs here]

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.7.12, on Ubuntu 22.04.2 LTS 5.19.0-41-generic, locale en_US.UTF-8)
    • Flutter version 3.7.12 on channel stable at /home/matias/develop/flutter/stable
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 4d9e56e694 (12 days ago), 2023-04-17 21:47:46 -0400
    • Engine revision 1a65d409c7
    • Dart version 2.19.6
    • DevTools version 2.20.1

[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.0)
    • Android SDK at /home/matias/Android/Sdk
    • Platform android-33, build-tools 33.0.0
    • Java binary at: /snap/android-studio/current/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /snap/bin/firefox

[✓] Linux toolchain - develop for Linux desktop
    • Ubuntu clang version 14.0.0-1ubuntu1
    • cmake version 3.22.1
    • ninja version 1.10.1
    • pkg-config version 0.29.2

[✓] Android Studio (version 2021.3)
    • Android Studio at /snap/android-studio/125/android-studio
    • Flutter plugin version 72.1.1
    • Dart plugin version 213.7433
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] Android Studio
    • Android Studio at /snap/android-studio/current/android-studio
    • 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
    • android-studio-dir = /snap/android-studio/current/android-studio
    • Java version OpenJDK Runtime Environment (build 11.0.13+0-b1751.21-8125866)

[✓] Connected device (3 available)
    • sdk gphone64 x86 64 (mobile) • emulator-5554 • android-x64    • Android 13 (API 33) (emulator)
    • Linux (desktop)              • linux         • linux-x64      • Ubuntu 22.04.2 LTS 5.19.0-41-generic
    • Chrome (web)                 • chrome        • web-javascript • Mozilla Firefox 112.0.1

[✓] HTTP Host Availability
    • All required HTTP hosts are available

• No issues found!

Additional information

  1. Maybe related with [Form] AutovalidateMode.onUserInteraction validates Textfields without interaction #107350 and the linked PR fix: Form AutovalidateMode.onUserInteraction behavior #120730
  2. I think that this error is because if not set autovalidateMode or set AutovalidateMode.disabled, is the same behavior. Can see in this line
    }) : autovalidateMode = autovalidateMode ?? AutovalidateMode.disabled;
  3. With currently behavior and definition, if by default autovalidateMode on FormField is disabled, the autovalidateMode property on Formparent never would be works because FormField property has priority with respect to Form

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 problemsf: material designflutter/packages/flutter/material repository.found in release: 3.10Found to occur in 3.10found in release: 3.7Found to occur in 3.7frameworkflutter/packages/flutter repository. See also f: labels.has reproducible stepsThe issue has been confirmed reproducible and is ready to work onteam-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