Long time ago I created my own version of an input Validator and formatter, inspired by @meredydd’s post.
Over time I have improved it and recently added Anvil Extra’s popovers as the optional way to show validation errors.
I am posting it here because I have used it for a long time in many apps and I think it’s mature enough for show time.
Here is the clone link for the demo app and the Validator dependency:
https://anvil.works/build#clone:6YJRNIYC6XTKYJ2E=7U3N2HLOGXAH3LIESKIBA34X
Here is the demo: https://stefano-validator-demo.anvil.app
When you start the demo you have two links on the navigation bar. One shows the validation in action using labels, the other using popovers.
A label shows the code used for the validation in front of each input component.
The code inside the demo is self-explanatory and shows how the validator is used.
Here is a quick preview:
from Validator.validator import Validator
def __init__(self, **properties):
self.init_components(**properties)
self.validator = Validator()
self.validator.between(component=self.text_box_1,
events=['lost_focus', 'change'],
min_value=5,
max_value=10,
include_min=True,
include_max=False)
self.validator.number(component=self.text_box_8,
events=['lost_focus', 'change'],
format='float 07.3f')
self.validator.with_function(component=self.text_box_13,
events=['lost_focus', 'change'],
validating_function=lambda tb: tb.text >= 'c',
message='Must be greater than "c"')
def check_all_click(self, **event_args):
print(self.validator.are_all_valid())