-
-
Notifications
You must be signed in to change notification settings - Fork 2
incendium.util.validate_form
César Román edited this page Mar 24, 2022
·
12 revisions
Performs a form validation.
Args:
- strings (dict): A dictionary containing all strings which must not be empty. Optional.
- numbers (dict): A dictionary containing all numbers which must be greater than zero. Optional.
- collections (dict): A dictionary containing all collections which must at least contain an element. Optional.
Returns:
- tuple[bool, str]: A tuple containing: is_valid (bool): True if all validation tests have passed, False otherwise. error_message (str): Error message in case any validation test has failed.
If you happen to have a Table or Power Table inside a Container, you may retrieve the title of the Container programmatically by doing the following:
<container>.getBorder().getTitle()
from incendium import util
# Constants.
ERROR_WINDOW_TITLE = "Error"
FORM_ERROR = "Please provide information in the following fields:"
# Initialize variables.
rootContainer = event.source.parent
# Labels, Text Boxes, and Spinners.
lblName = rootContainer.getComponent("lblName")
txtName = rootContainer.getComponent("txtName")
lblDescription = rootContainer.getComponent("lblDescription")
txtDescription = rootContainer.getComponent("txtDescription")
lblQuantity = rootContainer.getComponent("lblQuantity")
sprQuantity = rootContainer.getComponent("sprQuantity")
# Containers.
ctrContainer = rootContainer.getComponent("ctrContainer")
tblPowerTable = ctrContainer.getComponent("tblPowerTable")
# Required Strings.
strings = {
lblName.text: txtName.text,
lblDescription.text: txtDescription.text,
}
# Required Numbers.
numbers = {lblQuantity.text: sprQuantity.intValue}
# Required Collections.
collections = {
ctrContainer.getBorder().getTitle(): len(
system.dataset.toPyDataSet(tblPowerTable.data)
)
}
is_valid, error_message = util.validate_form(strings, numbers, collections)
if is_valid:
# TODO: Do something.
return 0
else:
util.info(FORM_ERROR, ERROR_WINDOW_TITLE, error_message)