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!: InputFilter clears TextField when an invalid character is entered #3779

Merged
merged 2 commits into from
Aug 18, 2024

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Aug 9, 2024

Description

Fixes #3769

A custom FilteringTextInputFormatter has been created to prevent non-matching characters from causing the deletion of the whole field content.

Breaking Change

Solving this issue resulted to a breaking change: most of the regex_strings must be updated to use anchors.

Why?

This is caused by RegExp.hasMatch() allowing non-matching characters to be entered/visible (without replacement), as long as the field content contains a matching substring.

Migration

Use start(^) and end($) anchors in the pattern/regexString:

  • r"[a-zA-Z]" -> r"^[a-zA-Z]*$"
  • r"[0-9]" -> r"^[0-9]*$"

In the above cases, the * is important, else it will be impossible to delete the last character in the field.

AI Prompt

To ease migration, below is a simple prompt that can be useful:

Help me update the following regex pattern: ####### 
Ensure the entire string matches the pattern and allows for an empty string. 

Test Code

import flet as ft


def main(page: ft.Page):

    page.add(
        ft.TextField(
            label="Ex: 2244.89",
            keyboard_type=ft.KeyboardType.NUMBER,
            input_filter=ft.InputFilter(
                allow=True,
                regex_string=r"^\d*(\.\d{0,2})?$",
                replacement_string="",
            ),
        )
    )


ft.app(target=main)

Type of change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Summary by Sourcery

Fix issue where InputFilter clears TextField when an invalid character is entered by introducing CustomFilteringTextInputFormatter. This change requires updating most regex_strings to use start (^) and end ($) anchors to ensure proper matching.

Bug Fixes:

  • Prevent InputFilter from clearing the entire TextField content when an invalid character is entered.

Enhancements:

  • Introduce CustomFilteringTextInputFormatter to handle input filtering with more control over regex patterns and matching behavior.

Copy link
Contributor

sourcery-ai bot commented Aug 9, 2024

Reviewer's Guide by Sourcery

This pull request addresses issue #3769 by introducing a custom FilteringTextInputFormatter to prevent invalid characters from clearing the entire TextField content. The changes include the creation of CustomFilteringTextInputFormatter and CustomNumberFormatter classes, updates to the InputFilter class to support additional regex options, and modifications to default filters to use anchored patterns. These changes result in a breaking change, requiring updates to existing regex_strings to use start (^) and end ($) anchors.

File-Level Changes

Files Changes
packages/flet/lib/src/utils/textfield.dart
packages/flet/lib/src/controls/textfield.dart
Introduced custom input formatters (CustomFilteringTextInputFormatter and CustomNumberFormatter) to handle specific filtering and formatting logic.
sdk/python/packages/flet-core/src/flet_core/textfield.py Enhanced InputFilter class to support additional regex options and updated default filters to use anchored patterns.

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.

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 @ndonkoHenri - I've reviewed your changes and found some issues that need to be addressed.

Blocking issues:

  • Hard-coded regex pattern found. (link)
  • Hard-coded regex pattern found. (link)
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🔴 Security: 2 blocking issues
  • 🟢 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 FeodorFitsner merged commit 268407f into main Aug 18, 2024
3 checks passed
@FeodorFitsner FeodorFitsner deleted the regexp branch August 20, 2024 18:04
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.

Fix InputFilter clearing field when an invalid character is entered
2 participants