-
Notifications
You must be signed in to change notification settings - Fork 490
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
Dynamic Validations in TextField
#631
Comments
There is no built-in validation mechanism into TextField control, but you can implement that in your program using |
Onchange is called after the keyboard value has been inserted into the field. It will look ugly to let the user see his text be inputted, then see it removed. Think of it...we need in-built validators or the ability to overwrite the method of TextField inserting the the text. See kivy example: https://kivy.org/doc/stable/api-kivy.uix.textinput.html#filtering |
@ndonkoHenri - I've tried a quick implementation of a filter by using The problem I had though was changing the Validation in the same way web browsers tend to do it, i.e. allowing you to type anything you want into the box, then checking it and simply complaining (as @FeodorFitsner is suggesting), and perhaps blocking any type of "submit" action looks to be a workable solution that users might be more familiar with. I know I would be more frustrated with an app for ignoring my input -- or worse, changing something I pasted in -- than it telling me the input isn't valid and letting me fix it (or even having a button to filter it for me after I've typed what I want). |
That's right. |
Yeah, you are trying to type something into the field and it doesn't respond and then after a few minutes you like "oh, it's a numeric field!". Agree, showing validation errors on blur or submit is more user friendly. |
@JWDT, would you please share your implementation with us? |
import flet as ft
def main(page: ft.Page):
def text_filter(e):
bad_chars = "1234567890"
output = ""
for char in e.control.value:
if char not in bad_chars:
output += char
e.control.value = output
page.update()
page.add(ft.TextField(helper_text="No numbers!", on_change=text_filter))
ft.app(target=main) Not sure on the efficiency of it since it's looping through the whole text value every time, but I didn't notice it leave the number characters in long enough to be able to read it. Again, this still shoots the cursor back to the start (only if it changes the |
def control_notas1(e): My TextField |
This is from chatgpt, what do you think @FeodorFitsner? :
|
How can I add validations in a
TextField
?My Use cases:
TextField
for username which won't allow space in a text.TextField
for number input which won't allow non-numeric input.The text was updated successfully, but these errors were encountered: