Skip to content

FormField widgets validated on every build() #36154

Closed
@audkar

Description

When FormField has autovalidate = true validator gets called on every widget build. My initial expectation was that it should happen only after value change. Documentation states similar case on final bool autovalidate;:

If true, form fields will validate and update their error text immediately after every change. Otherwise, you must call [FormState.validate] to validate.

This causes two problems:

  1. FormField immediately shows error on widget create.
  2. Validates and shows errors on all Form childs after first child widget value updated. Expectation is that only first Field gets validated which value got changed.

Steps to Reproduce

Validation error shown immediately on initial app start:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: <Widget>[
            Text('Problem1 (immediate error)'),
            TextFormField(
              validator: (value) => "error",
              autovalidate: true,
            ),
            Text('Problem2 (everything validated after first Field change):'),
            Form(
              autovalidate: true,
              child: Column(
                children: <Widget>[
                  TextFormField(
                    validator: (value) => "error",
                  ),
                  TextFormField(
                    validator: (value) => "error",
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }
}

Logs

[✓] Flutter (Channel stable, v1.7.8+hotfix.3, on Linux, locale en_US.UTF-8)
    • Flutter version 1.7.8+hotfix.3 at /.../flutter
    • Framework revision b712a172f9 (5 days ago), 2019-07-09 13:14:38 -0700
    • Engine revision 54ad777fd2
    • Dart version 2.4.0

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.1)
    • Android SDK at /.../android-sdk
    • Android NDK location not configured (optional; useful for native profiling
      support)
    • Platform android-29, build-tools 29.0.1
    • ANDROID_HOME = /.../android-sdk
    • Java binary at: /usr/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_222-b05)
    • All Android licenses accepted.

Metadata

Assignees

No one assigned

    Labels

    a: text inputEntering text in a text field or keyboard related problemsc: performanceRelates to speed or footprint issues (see "perf:" labels)frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions