Handler - Validator
Validator handler uses a Python expression in validating a value in the parent attribute. The handler evaluates the expression and displays an error if the expression returns a false value.
For information about Python, please visit www.python.org. A quick tutorial on Python can be found at www.python.org/doc/tut. Efecte uses a Python implementation called Jython. Jython specific documentation is available at www.jython.org.
Validator handler validates the value only when the data is created or edited through the user interface: a normal data card edit or by multiediting (multiedit since 4.1 4113). Note: When multi editing data cards, the validation may fail because other fields of the data card doesn't match the validation and preventing saving the data card. A check will not be performed if the source data is imported to Matrix42.
The data is considered valid if expression or script result is one of the following:
- Empty string (''
or""). - A number other than zero (0).
Jythonuses 1 instead of boolean value True. - Successful regular expression match.
Note the following error cases:
- A non-empty string is interpreted as an error message.
- 0 is invalid. That may be result of a boolean expression.
-
Noneis invalid. An unsuccessful regular expression match returnsNone.
You can use the Validator handler, for example, to compare the values of different attributes denoted by their individual attribute codes in the parent attribute's metadata.
Parent attribute value can be referred using the variable moniker _current.
For example, to ensure, that the parent attribute Amount is larger than Q2, attach the Validator handler to the attribute Amount and denote the following Python Validator in the metadata expression: _current>[attributeCodeQ2] . When saving, Validator handler evaluates the expression and returns an error to the user if the condition is not met.
The following tables provide information on the attribute settings and metadata requirements for Validator handler. Compulsory metadata is provided in a Required attribute metadata table.
Configuration of Validator handler varies from other handlers. Needed metadata names are given, admin must give the values only.
Table 1. Attribute Settings
| Handler name | Datatype |
|---|---|
Validator |
Any |
Table 2. Required Attribute Metadata
| Name | Value | Description |
|---|---|---|
expression |
[Python expression] |
A valid If the expression cannot be parsed, a referenced attribute value is empty or an unknown error occurs, the validation will pass and saving will proceed. |
defaultErrortext |
[text_validator_error] | The error text that will be returned to user if validation fails. This can be a plaintext or a presentation text code that is interpreted into user's selected language. text_validator_error is the default errorcode. |
isScript |
[on | off] |
When the value is Scripts are more powerful than expressions and can contain multiple lines, loops and other control structures. Also they are more complex to configure. Check examples of scripts below. If you want to interpret the expression as a script, check this box. The result value must be set into a variable called More example configurations available below. NOTE! This functionality may or may not be available on your system depending on licensing. |
invalidOnError |
[on | off] |
When the value is on, an error occurring when executing the expression will cause the attribute value to be interpreted as invalid. |
Table 3. Optional Attribute Metadata
| Name | Value | Description |
|---|---|---|
alwaysExecute |
true/false |
If this is true, the expression will be executed (as root) when the data card is saved and this attribute has a value. Default is false. |
Validator handler also allows access to referenced data cards. The current card can be referred with this, and other data cards by their the class attribute code configured in the parent card. Currently supported methods for referred data cards are:
Table 4. Data Card Access Methods
| Method | Returns | Example |
|---|---|---|
get(attributecode) |
Single value found in the field with the given attribute code. If the attribute is multivalued, first value is returned. If attribute is a reference, a PythonEntity object is returned. |
this.get("support_person").get("email") |
getAll(attributecode) |
Array containing values found in the field with the given attribute code. If attribute is a reference, an array of PythonEntity object is returned. |
max(getAll("product_prices")) |
Script Examples
Ensure that current attribute value is between 50 and 100:
50<_current<100Check that the length of the current field is exactly 8
len(_current)==8Lets assume: 1) "car" is a single value reference of _this_ template 2) "limit" an integer attribute of _car_ template 3) Check that current attribute value is less than the limit from single car reference
_current<car.get("car_limit")Decide the error message based on the current attribute and attribute with the code speed_limit. Requires isScript option:
if _current>speed_limit:
_result='The value must be smaller than the speed limit!'
elif _current<0:
_result='The value must be larger than zero!'Check that the current field matches a given regular expression, where the input must be of form EFECTE-nnnnnn, where n is a number:
re.match(r"EFECTE-\d{6}",_current)Check that the current field matches a given regular expression, where the input must start with an letter A and a number:
re.match(r"A\d.*",_current)If the jobstate attribute has value Closed and the travel_time or actual_work_time attribute is empty, display an error message.
errorText = ""
if (this.get("jobstate")=="Closed" and this.get("travel_time")==None):
errorText=errorText + "<br/>Please fill in the 'Travel time' field"
if (this.get("jobstate")=="Closed" and this.get("actual_work_time")==None):
errorText=errorText + "<br/>Please fill in the 'Actual work time' field"
if (errorText != "" ):
_result=errorTextTable of Contents