I know there’s a lot of validation modules out there already, but I’ve seeing a lot of "Show and Tell"s from people in the community and a lot of what I do most of the time recently is build up dependencies for my (mostly future) apps, so I wanted to show something that I did myself along all the years I’ve playing with anvil.
I recently updated my validation module in my main dependency to use m3 components and be more generic. I made a small app to test it and thought of sharing it here:
I added a lot of commentary to explain each operation in the clone.
A little summary of what the dependency have:
Validatable class
Forms that you want to be validated can inherit the Validatable
class. They will receive a few methods like:
set_required_components
: defines wich components must be filled.set_required_atttributes
: the same, but for attributes.set_conditional_component
: defines components that must be filled, but only when some conditions are True.is_valid
: checks if the form is valid and, if not, shows a Notification listing the erros and applies a role in the fields that are in error.
A Validatable form can have multiple groups for checking in different occasions. You can have validations for the entire form, but also for specific sections, like the fields to add to a grid.
Right now it can validate TextBox, TextArea, DropDown, DatePicker, DateGrids and RepeatingPanels, but it can also validate CustomComponents!
CustomValidationClass
If you have a custom component that needs to be validated, you can make it inherit the CustomValidationClass
. If you do, you can inform a callable that returns if the class is “valid” or not and also a text to show as an error message when it’s not. CustomValidationClass
instances will also be validated using Validatable
MapCompare
You can use MapCompare
to check if an object is already inside a RepeatingPanel using custom conditions.
When you create the MapCompare, you inform the panel and the list of attributes to check. When you want to check if it is_already_in
, you pass the item and it will check each item and it’s properties to see if there’s a similar item inside. The conditions can be indexes (when each item is a tuple), strings keys (when using dicts) or attribute names (for other types of objects).
Each attribute is checked using the AND operator. To be considered the same the row must have a value that match for ALL listed attributes.
You can also inform a list of attributes as ONE of the attributes in the list to make a check using the OR operator. Then, at least one of the values must match for that group.
Let’s say you have a Grid with 3 columns: “Name, ID, Mother”. You can use map compare to not allow two items with the same ID to enter the grid. Or to see if there’s another entry with the same Name and Mother.
This is actually just a part of my core dependency that all my apps depend on. I extracted to show to you all! Let me know what you think or if you have suggestions!