Often, when you’re collecting data with a form, you’ll need certain input before you can continue. Some fields are mandatory, sometimes a date must be in the future, and sometimes the user just has to check a confirmation box before they can proceed.
I’ve written a little library to make validating your input easier. All you need to do is design your form, place Labels for your error messages (eg a Label that says “you must enter a username”), and write a few lines of code to define what you require for validation:
def __init__(self, **properties):
self.init_components(**properties)
v = form_checker.validation.Validator()
# Make text_box_1 mandatory, and show err_1_lbl if it's not filled out
v.require_text_box(self.text_box_1, self.err_1_lbl)
# submit_button is only enabled when you've filled everything out:
v.enable_when_valid(self.submit_button)
The Validator will automatically show the error labels for components that aren’t valid. (You can even choose whether to show the message immediately, or only after the user interacts with that component.)
It’s pretty flexible - you can define your own conditions for what counts as valid input, and what events trigger a check.
Click here to get the library, and see a working example:
https://anvil.works/ide#clone:PAIXGBNAS4VRHKCU=4QQLTK7KC6L63L262AW3ESDT
Note: This library is only for use in client-side code, so it can be bypassed by a malicious user. It’s a user-interface tool; don’t rely on this to keep malicious data out of your app!