TLDR; I made Python decorators that enforce some default input validation for TextBox and TextArea form elements.
Client side form validation is a pain the the butt, but… we need to do it in order to make sure our databases don’t get corrupt data, or that we aren’t calling back-end server functions with missing / corrupted data… So instead of manually writing the code to do validation, why not write code to write that code for us?
This is where Python decorators come in. Essentially, we can decorate some sort of submit_button_click
method with a decorator. This decorator is smart enough to pull off types of TextBox
OR TextArea
instances variables, and only execute the body of the submit_button_click
code when it knows all of the input elements have valid input
I understand that this current method isn’t very robust to change, and is limited, so I’ll be updating it in the future. The linked app is just a proof of concept, not a finished product.
In the future I’d like some way to link a Form element with a lambda expression passed in via the decorator to make this more general.
Here’s the sample project in case anyone is interested in helping me make this more robust / make the existing code less of a dumpster fire.
https://anvil.works/build#clone:EB3ZTENIVHPRGODI=TUKKAUGZXTTTFUJUAKUAUXW2