Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unexpected behavior on TextField showing error when errorText is empty #3855

Conversation

andersou
Copy link
Contributor

@andersou andersou commented Aug 23, 2024

Description

When the page is refreshed, the input field is incorrectly marked as an error if errorText is set previosly. This happens because _set_attr_internal prevents the errorText attribute from being set to None, resulting in it being set as an empty string, which causes the error border to appear.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • [x ] I signed the CLA.
  • [ x] My code follows the style guidelines of this project
  • [ x] I have performed a self-review of my own code
  • [x ] I have commented my code, particularly in hard-to-understand areas
  • [ x] My changes generate no new warnings
  • [x ] New and existing tests pass locally with my changes
  • [ x] I have made corresponding changes to the documentation (if applicable)

Screenshots (if applicable):

image
image

after page refresh:

image

Summary by Sourcery

Fix the input field error display issue by setting errorText to null when it is empty, preventing unnecessary error borders.

Bug Fixes:

  • Fix the issue where an input field is incorrectly marked as an error when the errorText is empty by ensuring the errorText attribute is set to null instead of an empty string.

Copy link
Contributor

sourcery-ai bot commented Aug 23, 2024

Reviewer's Guide by Sourcery

This pull request fixes a bug in the input field error display. The main change is in the buildInputDecoration function, where the errorText property is now set to null when it's an empty string, preventing the error border from appearing unnecessarily after a page refresh.

File-Level Changes

Change Details Files
Modified error text handling in input field decoration
  • Changed errorText assignment to use a ternary operator
  • Set errorText to null when the attribute string is empty
  • Maintained original behavior when errorText is not empty
packages/flet/lib/src/utils/form_field.dart

Tips
  • Trigger a new Sourcery review by commenting @sourcery-ai review on the pull request.
  • Continue your discussion with Sourcery by replying directly to review comments.
  • You can change your review settings at any time by accessing your dashboard:
    • Enable or disable the Sourcery-generated pull request summary or reviewer's guide;
    • Change the review language;
  • You can always contact us if you have any questions or feedback.

@andersou andersou changed the title fix: not error on inputfield when errorText is empty Fix unexpected behavior on TextField showing error when errorText is empty Aug 23, 2024
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @andersou - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding a unit test to verify this behavior and prevent future regressions.
  • It might be helpful to add a brief comment explaining the rationale behind this change for future maintainers.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment to tell me if it was helpful.

@FeodorFitsner
Copy link
Contributor

Could you include a simple repro code/app to validate the fix?

@andersou
Copy link
Contributor Author

@FeodorFitsner

import flet as ft

import logging

logging.basicConfig(level=logging.DEBUG)


def main(page: ft.Page):
    txt = ft.TextField(label="Endereço de IP")

    def validate(e):
        if not txt.value:
            txt.error_text = "Campo obrigatório"
        else:
            txt.error_text = None
        txt.update()

    btn = ft.ElevatedButton("Enviar", on_click=validate)
    page.add(txt)
    page.add(btn)


ft.app(target=main)

run the app with flet run --web
click in the button, the validation will appears
insert any text in the textfield, and click the button, error text will disappear
when refresh this page, the inputtext will have an error border

@FeodorFitsner FeodorFitsner merged commit e4eb1e9 into flet-dev:main Aug 27, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants