-
-
Notifications
You must be signed in to change notification settings - Fork 2
incendium.gui.validate_form
César Román edited this page Apr 30, 2024
·
1 revision
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 __future__ import print_function
import system.dataset
import incendium.gui
import incendium.vision.gui
# 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 = incendium.gui.validate_form(strings, numbers, collections)
if is_valid:
# TODO: Do something.
print(1)
else:
incendium.vision.gui.info(FORM_ERROR, ERROR_WINDOW_TITLE, error_message)