Skip to content

Formulas

tomolimo edited this page Nov 23, 2021 · 21 revisions
  1. A formula is a pure JavaScript boolean expression which evaluates to true or false. If true the field is validated, if false the field is not validated.
  2. If a field formula evaluates to true then the field is valid and if all the fields are valid and if the form formula is valid, then the form is submitted.
  3. If at least one field formula is evaluated to false, then an alert is shown and the form is not submitted.
  4. The alert message contains the labels of all the fields which evaluated to false. Or alert message contains a dedicated error message per field. This dedicated error message can be added into the /plugins/formvalidation/locales/ folder, see this folder and files en_GB.php and fr_FR.php.
  5. In a formula, a field value is referred to by a # (to refer value of current field) or #xxx (to refer value of field with id is xxx). There is no need to add any attribute to the field reference to get its value. # will return the current value (or checked state when a checkbox is referred by the #) of the field. Ex: for a text field # will return the value (i.e. a string), for a checkbox it will return the checked attribute (i.e. true or false). More information about field values on Default Values.
  6. An empty (=empty string '') formula will be auto-validated as # > 0 if field is a dropdown or as # != '' for any other field type. More information about default evaluation on Default Formulas.
  7. Some Helper Functions have been added to ease field validations. Regular expressions can also be used, for example: /^\\d+\$/.test(#) to validate that current field is a valid integer (note the \\d in order to escape the \d).
  8. Field formula example: (#=='' && #57==0) || (#!='' && FVH.isValidDate(#) && #57==1 ) means current field is valid if (value of current field is an empty string AND value of field 57 equals 0) OR (value of current field is not empty string AND value of current field is a valid date AND value of field 57 equals 1). See Helper Functions to get explanations on FVH.isValidDate().

Example

Let's use the same example than for Mandatory Formulas:

Let's imagine that when 'Simplified Interface' at ticket creation you would like to force requester to input a ticket description when ticket is of type 'Request', and that this description must be at least of 10 chars and that it must contains a minimum of 5 words.

And this time concentrate on the field formula of the 'Description' field. The formulas can be a bit complex (but are very easy to understand) as it depends on the field #11 (the 'Type') and on the content of 'Description' itself:

(#11 == 1) || (#11 == 2 && #.length > 10 && FVH.countWords(#) > 5 )

It means 'Description is valid if either 'Type' (#11) is 'Incident' (=1) or if 'Type' is 'Request' (=2) AND 'Description' length is more than 10 chars AND nb words in 'Description' is greater than 5.

Clone this wiki locally