Skip to content

Validation with dependency between fields did not happen as expected #74765

@ErliSoares

Description

@ErliSoares

Steps to Reproduce

  1. Run the code below
  2. Click the validate button
  3. Uncheck the "Field Is Required" checkbox
code sample
import 'package:flutter/material.dart';

void main() {
  runApp(
    MaterialApp(
      home: MyWidget(),
    ),
  );
}

class MyWidget extends StatefulWidget {
  @override
  _MyWidgetState createState() => _MyWidgetState();
}

class _MyWidgetState extends State<MyWidget> {
  GlobalKey<FormState> _formKey;
  bool _required = true;

  @override
  void initState() {
    super.initState();
    _formKey = GlobalKey<FormState>();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Form(
        key: _formKey,
        child: Column(
          children: [
            CheckboxListTile(
              title: const Text('Field Is Required'),
              value: _required,
              onChanged: (value) {
                setState(() {
                  _required = value;
                });
              },
            ),
            RaisedButton(
              child: const Text('Validate'),
              onPressed: () {
                _formKey.currentState.validate();
              },
            ),
            TextFormField(
              autovalidateMode: AutovalidateMode.onUserInteraction,
              decoration: const InputDecoration(labelText: 'Field 1'),
              validator: (value) {
                if (value == '' && _required) {
                  return 'Error';
                }
                return null;
              },
            ),
          ],
        ),
      ),
    );
  }
}

Expected results: As the field looks like AutovalidateMode.onUserInteraction I expected the field to be revalidated and the error removed.

I had to create another component for the field to self-validate, something like _hasInteractedByUser = true; in the block below would solve

bool validate() {
setState(() {
_validate();
});
return !hasError;
}

Actual results: As the field has not been changed by the user, the field is not revalidated.

flutter doctor -v
[√] Flutter (Channel stable, 1.22.5, on Microsoft Windows [Version 10.0.19042.746], locale pt-BR)
    • Flutter version 1.22.5 at C:\src\flutter
    • Framework revision 7891006299 (7 weeks ago), 2020-12-10 11:54:40 -0800
    • Engine revision ae90085a84
    • Dart version 2.10.4


[√] Android toolchain - develop for Android devices (Android SDK version 30.0.0-rc1)
    • Android SDK at D:\AndroidSdk
    • Platform android-30, build-tools 30.0.0-rc1
    • ANDROID_HOME = D:\AndroidSdk
    • ANDROID_SDK_ROOT = D:\AndroidSdk
    • Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
    • All Android licenses accepted.

[!] Android Studio (version 4.1.0)
    • Android Studio at C:\Program Files\Android\Android Studio
    X Flutter plugin not installed; this adds Flutter specific functionality.
    X Dart plugin not installed; this adds Dart specific functionality.
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[!] VS Code (version 1.52.1)
    • VS Code at C:\Users\erli\AppData\Local\Programs\Microsoft VS Code
    X Flutter extension not installed; install from
      https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (1 available)
    • SM M315F (mobile) • RX8N80DL2NK • android-arm64 • Android 10 (API 29)

! Doctor found issues in 2 categories.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions