TextInput on_change and validators working together #2323
-
In this minimum working example I tried to enable/disable a button based on a import toga
from toga.validators import Number
class MainApp(toga.App):
def startup(self):
self.main_window = toga.MainWindow(title=self.formal_name)
self.button = toga.Button("submit")
text_input = toga.TextInput(on_change=self.set_button, validators=[Number()])
self.main_window.content = toga.Box(children=[text_input, self.button])
self.main_window.show()
def set_button(self, widget: toga.TextInput):
self.button.enabled = widget.is_valid
def main():
return MainApp("App", "com.example")
if __name__ == "__main__":
main().main_loop() My goal is to disable the button whenever the text is not valid. def my_validator(self, input_string):
try:
float(input_string)
self.button.enabled = True
return None
except ValueError:
self.button.enabled = False
return "Input should be an integer" |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
That's an interesting problem. The evaluation of validation after the change signal isn't an explicitly intentional feature, but it is consistently implemented that way across all platforms (e.g., here's the macOS implementation - the same pattern is used on every backend.) I agree that it would make sense for the "is valid" indicator to be correct when the change handler is invoked - you've identified at least one obvious use case; and I can't think of any use case that is made possible by the current behavior. I'll open a ticket for this feature request. As an immediate workaround, you could invoke the validation logic as the first thing done by the |
Beta Was this translation helpful? Give feedback.
That's an interesting problem. The evaluation of validation after the change signal isn't an explicitly intentional feature, but it is consistently implemented that way across all platforms (e.g., here's the macOS implementation - the same pattern is used on every backend.)
I agree that it would make sense for the "is valid" indicator to be correct when the change handler is invoked - you've identified at least one obvious use case; and I can't think of any use case that is made possible by the current behavior. I'll open a ticket for this feature request.
As an immediate workaround, you could invoke the validation logic as the first thing done by the
on-change
handler - (i.e.,widget._va…