Form Validation Rules and Calculated Fields

Form Validation Rules and Calculated Fields

When using forms in JMap to enter data, some business logic is often needed. This logic may be translated into form validation rules that are more or less complex. For example, in the case of an asset inspection form, it is possible that the sum of 3 numeric fields must not exceed a total of 100%. With JMap’s new form validation rules, it is now possible to implement this type of logic, and so much more. In this article, we will go over the different possibilities that are offered.

Global form validation rules

These rules are defined at the form level and are evaluated before the form is saved. In order for the form to be saved, all of the rules must pass; in other words, they must be validated as TRUE. Each rule can use the values of one or more fields in the form. Lastly, several distinct rules can be created for a single form.

To define a rule, you must enter a name, a logical expression, and a message to be displayed when the rule fails.

When the user enters data in the form’s fields and a rule fails, the non-compliance message is displayed.

La syntaxe utilisée pour définir une règle logique est celle de JsonLogic. JsonLogic est un langage permettant de définir des expressions logiques et d’effectuer des opérations mathématiques et sur des chaînes de caractères. La syntaxe générale d’une règle est la suivante :

{"operator" : ["values" ... ]}.

In the definition of the rules, the attribute values of the layers’ elements are accessed using the following syntax:

"var" : "NAME OF ATTRIBUTE".

Here are some examples:

In this example, the sum of the values of attributes A, B and C must not be greater than 100.

In this example, if the value of the STATUS field is Done, then the MATERIAL field cannot be left empty, and the INSP_DATE field cannot contain the null value.

Automatically calculated values

In addition to validation rules, JsonLogic expressions can be used to automatically calculate and populate data in the form’s fields using the values of its other fields. For example, we could calculate the value of field D by adding together the values of fields A, B and C.

Here are some examples of calculated values:

In this example, the sum of the values from fields A, B and C will be automatically populated as the calculated value.

In this example, the calculated value that will be automatically entered is 2 x PI x the value of the DIAMETER field divided by 2. This is the formula for the circumference of a circle (2 x Pi x R, where R is the radius).

Lastly, in this example, the field will automatically contain the “freeze” value if the value of the TEMP field is below 0. Otherwise, its value will be “no freeze.”

Rules defining the properties of the form fields

The Read Only and Required properties of the form fields can also be controlled using logical rules. These optional rules are defined at the field level for each field in the form. For example, we could determine that field B is only required if field A is populated.

As you can see, adding logical rules and calculated fields in JMap’s forms allows you to create smarter forms, which are better adapted to business rules that are often necessary. The possibilities of these rules are limited only by the imagination of the person who writes them.

Jean-Robert Desbiens-Haddad
CTO

Back to Technical Blog