Skip to content

incendium.util.validate_form

Cesar Roman edited this page Sep 23, 2020 · 12 revisions

Description

Performs a form validation.

Syntax

validate_form([strings, ][numbers, ][collections])

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: 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.

Recommendations

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()

Code Examples

# Imports.
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:
    # Do something.
else:
    util.info(FORM_ERROR, ERROR_WINDOW_TITLE, error_message)
Clone this wiki locally