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

Feature: TextField Input validation #2101

Merged
merged 6 commits into from
Nov 20, 2023
Merged

Feature: TextField Input validation #2101

merged 6 commits into from
Nov 20, 2023

Conversation

ndonkoHenri
Copy link
Contributor

@ndonkoHenri ndonkoHenri commented Nov 18, 2023

Close #631

Props:

@dataclasses.dataclass
class InputFilter:
    regex_string: str
    allow: bool = field(default=True)
    replacement_string: str = field(default="")

Example code:

import flet as ft


def main(page: ft.Page):
    page.title = "Input Filtering"
    page.add(
        ft.TextField(
            label="Only numbers are allowed :)",
            input_filter=ft.InputFilter(allow=True, regex_string=r"[0-9]"), # only allow numbers
        )
    )

ft.app(target=main)

Capture:
(when trying to add text characters they are rejected)
example

Notes:

  • regex_string can't be passed to dart as a raw string(using the 'r')
    RegExp(regexString ?? "")
    As seen above, this is because the value of regex_string is stored in a variable (regexString).. hence using string interpolation(RegExp(r"$regexString")) for example, will cant work because the variable's name will not be interpolated (we are in raw mode).
    From RegExp docs:

Use a raw string to treat each character, including \ and $, in a string as a literal character. Each character then gets passed to the RegExp parser. You should use a raw string as the argument to the RegExp constructor.

This is my first dart contribution, hence will strongly appreciate opinions. 😄

@FeodorFitsner
Copy link
Contributor

Well, looks like it's ready to merge, huh?

@ndonkoHenri
Copy link
Contributor Author

Yh, now I think its ready!

@FeodorFitsner FeodorFitsner merged commit 7f89312 into flet-dev:main Nov 20, 2023
@bobwatcherx
Copy link

i get eror for use inputfilter

Unhandled error processing page session : Traceback (most recent call last):
  File "/usr/local/lib/python3.10/dist-packages/flet_runtime/app.py", line 363, in on_session_created
    session_handler(page)
  File "/home/minx/belajar/fletval/main.py", line 9, in main
    input_filter=ft.InputFilter(allow=True, regex_string=r"[0-9]", replacement_string=""),
AttributeError: module 'flet' has no attribute 'InputFilter'

flet version

root@minx-Lenovo-V145-14AST:/home/minx/belajar/fletval# flet --version
0.12.2

code

import flet as ft


def main(page: ft.Page):
    page.title = "Input Filtering"
    page.add(
        ft.TextField(
    label="Only numbers are allowed :)",
    input_filter=ft.InputFilter(allow=True, regex_string=r"[0-9]", replacement_string=""),
) 
   )

ft.app(target=main)

@ndonkoHenri
Copy link
Contributor Author

ndonkoHenri commented Nov 22, 2023

You need to have the latest prerelease.

pip install flet --pre

An official release is still to come.

zrr1999 pushed a commit to zrr1999/flet that referenced this pull request Jul 17, 2024
* add ´InputFilter´

* create textfield utils

* update textfield.dart

* update return type

* remove `regex_string` default

* add predefined InputFilters
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.

Dynamic Validations in TextField
3 participants