Skip to content

Solution Proposal for showing or not the errorText in the TextFormField #52634

Closed

Description

@psygo

Use case

It would be nice to have a TextFormField that didn't show the error messages below when trying to do validation. For example, in my design, I chose to show the error messages interactively on the labelText.

Having no option of disabling the errorText can really be annoying when trying to deal with the spacing of the fields. You will either have to create an extra-padded TextFormField and let the errorText be always ' ', or live with the abrupt transition of when the error message appears and the spacing changes.

This has appeared in complaints before:

Proposal

I don't have a good solution for the abrupt transition when the errorText appears. Maybe showing it with some sort of AnimatedContainer would be a good option. (One solution that goes along those lines is this one).

However, when it comes to showing or not the errorText, I have a suggestion — also featured in this SO answer — that would change only 2 lines of the source's TextFormField:

  1. Add another parameter to the TextFormField's constructor (below this line), say, errorTextPresent:
    // `true` is the current implicit default, i.e., showing the `errorText`
    bool errorTextPresent = true 
  2. Change the decoration's initialization of the internal TextField to feature the new option.
    1. From:
      decoration: effectiveDecoration.copyWith(errorText: field.errorText),
    2. To:
      decoration: effectiveDecoration.copyWith(
          errorText: errorTextPresent ? field.errorText : null)
  3. You can now call TextFormField with a new option when you wish to disable the errorText:
    TextFormFieldWithErrorTextOption(
      errorTextPresent: false, // `false` will disable the `errorText`
      ...
    ),

Strictly speaking, this issue should probably have been a PR. But I chose it to be an issue because I didn't really know how the Flutter team would have liked to proceed.

Activity

  1. added
    a: text inputEntering text in a text field or keyboard related problems
    p: material_uimaterial_ui package in flutter/packages
    frameworkflutter/packages/flutter repository. See also f: labels.
    c: proposalA detailed proposal for a change to Flutter
    on Mar 16, 2020
  2. justinmc commented on Mar 19, 2020

    @justinmc
    Contributor

    How does this proposal solve the problem of a jarring height change when the error text appears? Do you just want the field to turn red but never show error text?

  3. psygo commented on Mar 20, 2020

    @psygo
    Author

    How does this proposal solve the problem of a jarring height change when the error text appears?

    I'm not sure if I'm missing something from the question, but my solution simply disables the errorText, so there wouldn't be any height change altogether.

    Do you just want the field to turn red but never show error text?

    My complete solution for fixing the height change has more to it than simply disabling the errorText below the field, but that's a solid step and a nice option to have anyway. My final design would feature the labelText changing to show the error — this could be a future nice feature of TextFormField in my opinion, i.e., where to show the errorText —; this way there would be no height change. Of course, I would have plenty of redundant design elements to still show the user the meaning of the field when the labelText has changed to an error message.

    A simple sample — in this case, the labelText would be the string "password" initially —:

    image

  4. justinmc commented on Mar 20, 2020

    @justinmc
    Contributor

    Ok got it. Could you post a minimal app that shows the problem if you have time? It would be helpful to see a full example. I wasn't even thinking of bordered text fields at first.

  5. psygo commented on Mar 21, 2020

    @psygo
    Author

    I've been working on a project/package for authentication with Flutter (flutter_firebase_auth_benchmark) in the last 2 weeks. The image I shared above is from it. You can check out my implementation of what I suggested in it.

    The widget with the mentioned features is the AuthTextFormField, which returns my modified version of the TextFormField, which I called TextFormFieldWithErrorTextOption for the moment.

    The project is a way for me to learn more about Flutter and Dart, besides serving a great entrypoint for later apps, since authentication is so ubiquitous.

    With respect to the project's architecture, I would say I encountered quite some problems which would change my approach completely if I were to start it over again. You can find out more about the problems in the README.

  6. justinmc commented on Mar 25, 2020

    @justinmc
    Contributor

    Thanks for the explanation and for taking the time to write out all that feedback in the README.

    For the record, another solution to this problem is to return an empty string from validator and then set the height of the error to zero:

    TextFormField(
      decoration: const InputDecoration(
        errorStyle: TextStyle(height: 0),
      ),
      validator: (value) => value.isEmpty ? '' : null,
    ),

    That's not really an ideal solution, though. We might need to make some bigger changes to Flutter's form validation, there was another PR recently with some good discussion about this (#48155). I hesitate to add new parameters like errorTextPresent since it's already pretty complicated, though it definitely would make this use case a lot cleaner.

  7. psygo commented on Mar 26, 2020

    @psygo
    Author

    there was another PR recently with some good discussion about this (#48155)

    Really interesting PR indeed.


    There are plenty of other solutions for this problem, I guess. I'm not a huge fan of the one you mentioned, though, because you seem to be turning the validator's usefulness down.

    Another solution I was thinking about lately was to put this boolean inside InputDecoration instead, since showing or not the error feels more like a decoration choice than functionality itself, and it might make things easier to understand for the user.


    Quite frankly, as a Flutter user, I'm not very satisfied with the TextFormField's design overall, specially the error text placement. There are so many modern designs which are so much more appealing. These should be very easy to implement, I think; we should not have to rework dozens of things. Take a look at this one for example, it even features animations, which is something some of the basic Flutter widgets already do have.

    On a sidenote, if there is interest in reworking the TextFormField, I would really suggest adding yet another parameter — to InputDecoration probably — for how many characters it takes before the ellipsis character appears on the labelText. I have created an issue about this: #53166. The solution to it seems to be buried deep down in some of the core renderers, but I'm not sure about it.

  8. justinmc commented on Mar 27, 2020

    @justinmc
    Contributor

    Flutter's Material library needs to follow the Material spec (see "Error text" in the Material docs). However, we should make it easy for developers to customize our widgets. Sometimes it is kind of like Material/Cupertino or nothing.

    I'll take a look at the ellipsis issue.

  9. PollyGlot commented on Sep 15, 2020

    @PollyGlot

    It would be nice to see some updates on this issue/proposal as I am also currently working with the task where I don't show an error text bellow the textField's

  10. justinmc commented on Sep 15, 2020

    @justinmc
    Contributor

    A PR was recently merged that introduces the autovalidateMode property to form fields. Would that solve the problem here?

  11. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Sep 15, 2020
  12. markusaksli-nc commented on Oct 7, 2020

    @markusaksli-nc
    Contributor

    Closing due to timeout.

    Let us know if there is anything still to be done here.

  13. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on Oct 7, 2020
  14. homelyng commented on Apr 8, 2021

    @homelyng

    Closing due to timeout.

    Let us know if there is anything still to be done here.

    Everything still needs to be done here @markusaksli-nc

    This is still a problem for many flutter ui developers.

    also see #54104 (comment)

  15. markusaksli-nc commented on Apr 12, 2021

    @markusaksli-nc
    Contributor

    So the autovalidateMode property didn't solve this?

  16. vidklopcic commented on Apr 22, 2021

    @vidklopcic

    I've been dealing with this issue for more than two years now. It's simply unacceptable that we still don't have any normal solution for this. I've seen and used everything from OverflowBox to manual borderStyle changes...

    Setting height: 0 and returning empty error message worked for a while, but it doesn't on the latest master anymore.

  17. naveenyadav15 commented on Jun 1, 2021

    @naveenyadav15

    Thanks for the explanation and for taking the time to write out all that feedback in the README.

    For the record, another solution to this problem is to return an empty string from validator and then set the height of the error to zero:

    TextFormField(
      decoration: const InputDecoration(
        errorStyle: TextStyle(height: 0),
      ),
      validator: (value) => value.isEmpty ? '' : null,
    ),

    That's not really an ideal solution, though. We might need to make some bigger changes to Flutter's form validation, there was another PR recently with some good discussion about this (#48155). I hesitate to add new parameters like errorTextPresent since it's already pretty complicated, though it definitely would make this use case a lot cleaner.

    By using this method the error is showing on top of the TextFormField.

    So we can use this instead

      errorStyle: TextStyle(
                        height: 0,
                        color: Colors.transparent,
                      ),
    
  18. github-actions commented on Aug 1, 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.

  19. locked as resolved and limited conversation to collaborators on Aug 1, 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

    a: text inputEntering text in a text field or keyboard related problemsc: proposalA detailed proposal for a change to Flutterframeworkflutter/packages/flutter repository. See also f: labels.p: 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