Skip to content

FormState validation method to return a Map of the validation status of children FormField(s) #135363

Description

@SharbelOkzan

Is there an existing issue for this?

Use case

In a screen containing a relatively long Form in a SingleChildScrollView, we want to bring the user's attention to the field that may cause a validation error after calling FormState.validate(). The solution was as follows:

  void _validateForm(BuildContext context) {
    if (_fomKey.currentState!.validate()) {
      fun();
    } else {
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (_someWidgetKey.currentContext != null && _someWidgetKey.currentContext!.mounted) {
          Scrollable.ensureVisible(
            _someWidgetKey.currentContext!,
          );
        }
      });
    }
  }

Now the problem is that where there are multiple widgets that may cause a validation error. There's currently no way to detect which one we should scroll to.
The only solution is to implement a custom validation logic, which defies the whole purpose of using Form

Proposal

The proposal is to have another version of the validate method, let's call it, validateGranularly (naming suggestions are very welcome) that returns Map<Key, bool>, so the above problem will be addressed as follows:

  void _validateForm(BuildContext context) {
    if (_fomKey.currentState!.validate()) {
      fun();
    } else {
      Key invalidFieldKey =  
      _fomKey.currentState!.validateGranularly().firstWehere((e) => !e) // e==false
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (invalidFieldKey .currentContext != null && invalidFieldKey .currentContext!.mounted) {
          Scrollable.ensureVisible(
            invalidFieldKey .currentContext!,
          );
        }
      });
    }
  }

Activity

  1. added
    in triagePresently being triaged by the triage team
    c: proposalA detailed proposal for a change to Flutter
    c: new featureNothing broken; request for a new capability
    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
    team-designOwned by Design Languages team
    and removed
    in triagePresently being triaged by the triage team
    on Sep 25, 2023
  2. SharbelOkzan commented on Sep 25, 2023

    @SharbelOkzan
    ContributorAuthor

    I've come across the same feature request #65573 but without a clear explanation of how this feature adds complexity to the form validation logic. The reasons could be outdated by now.

    I've also come across a relatively similar proposal for the Form.submit() that has been closed for the reason stated here. I don't see those reasons holding for this issue as validation results aren't expected to be serializable.

  3. added
    triaged-designTriaged by Design Languages team
    P2Important issues not at the top of the work list
    on Sep 28, 2023
  4. Renzo-Olivares commented on Sep 28, 2023

    @Renzo-Olivares
    Contributor

    Hi @SharbelOkzan thanks for proposing this new API. Your use-case sounds reasonable and we would welcome a PR which looks like you have already started #135578 so thanks!

  5. Renzo-Olivares commented on Jan 10, 2024

    @Renzo-Olivares
    Contributor

    Closing this as #135578 has been merged.

  6. github-actions commented on Jan 24, 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.

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

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: text inputEntering text in a text field or keyboard related problemsc: new featureNothing broken; request for a new capabilityc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.p: material_uimaterial_ui package in flutter/packagesteam-designOwned by Design Languages teamtriaged-designTriaged by Design Languages team

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions