Advanced Validations
Last updated
Last updated
The Validation Expressions section allows you to configure data logic validations, which are especially efficient for verifying that the value entered by the user in each field matches the expected data type. Its functionality consists of checking whether a specific condition is met.
If the condition is met and the result is true, an error message is displayed, and the user cannot submit the form until it is corrected. If the condition is false, no action will be taken, and the user can proceed with the process.
These types of validations can refer to the data entered in the field where they are defined (e.g., checking if it meets a specific length), in another field (e.g., showing an error in the "Quantity" field if the "Product" field is invalid), or by comparing data between two fields (e.g., checking if the start date for a given period is later than the end date).
Each expression consists of two values:
Message
The text to be displayed to the user to warn of the error when the validated condition is met, preventing the form from being submitted
Expression
The associated formula that will execute the validation for which the message is displayed
The Add Expression option in the top-right corner of the section allows you to include additional functions, enabling new fields for messages and expressions. This is useful for validating multiple conditions within the same field, such as ensuring a date falls within a specific period, is more recent than the date in another field, and is different from a third field. Applying multiple formulas with specific criteria allows for more precise control over the type of data accepted, helping to reduce the margin of error.
Below, we will analyze the following expressions in detail:
isEmpty(Field)
Content Validation
Returns true (error message) if a field is empty
count(Table)==0
Content Validation
Returns true if the rows in a table are empty
sum(Table,Column)!=N
Content Validation
Returns true if the total of a column is different from the allowed value
dateDifferenceInBusinessDays(Date1,Date2)<=N
Content Validation
Returns true if the number of business days is less than the rule allows
equal(Field1,Field2)
Comparison Validation
Returns true if two fields have the same value
smaller(Date1,Date2)
Comparison Validation
Returns true if the first date is earlier than the second
larger(Date1,Date2)
Comparison Validation
Returns true if the first date is later than the second
smallerEq(Date1,Date2)
Comparison Validation
Returns true if the first date is earlier than or equal to the second
largerEq(Date1,Date2)
Comparison Validation
Returns true if the first date is later than or equal to the second
not(Field)
Logical Operator Validation
Returns true if the field has a negative result
not(Expression)
Logical Operator Validation
Returns true if the expression has a negative result
and(Expression1,Expression2)
Logical Operator Validation
Returns true if both expressions are true
or(Expression1,Expression2)
Logical Operator Validation
Returns true if one of the two expressions is true
ConditionalExpression?TrueConditionExpression:FalseConditionExpression
Ternary Operator Validation
Returns the result of a second expression depending on whether the first condition is met
Keep in mind that formula components are enclosed in parentheses. The editor will display an error message if the number of open parentheses in the formula does not match the number of closed parentheses, helping you identify inconsistencies in more complex functions.
Certain formulas allow us to evaluate the validity of data based on whether the values entered meet specific characteristics established for that field, such as ensuring it is not blank or does not exceed certain limits. Let’s explore some of these and their functionality below.
As we saw earlier, validation expressions allow us to enhance certain functionalities of our form, such as configuring required fields. Below, we will use the isEmpty() expression to define a field as mandatory and display a custom error message when the user attempts to submit it blank.
Select the "Last Name and First Name" field and set the Required property to False to disable it. Then, go to the Validation Expressions section.
In the Message field, write the text that will be displayed to the user if they do not complete the field, for example: "You must enter your first and last name."
Next, you will need to set the formula that controls this condition. Click the fx icon on the far right of the Expression field to open the editor and enter the formula:
isEmpty(last_name_first_name)
Whenever you add an expression, make sure to verify that the field names are written correctly, respecting uppercase and lowercase letters. The editor will display a warning if a field name is not present in the schema when an undeclared value is entered, but it cannot detect if you have written the name of another existing field instead.
Once you have added the expression, click Confirm to finish. If the condition is met, meaning the "Last Name and First Name" field is blank, the function will return the value true and display the message "You must enter your first and last name," preventing the user from submitting the form until this data is entered. Conversely, if the condition is not met because the field is complete, it will return the value false, and the user will be able to submit the form successfully.
This type of validation can also be applied to tables to verify if rows are loaded or to Attachment fields to check if a file has been attached. If no information is present, the formula will return the value true and display an error message.
The count() formula is useful for counting the number of characters entered in a field and, when applied to a table, allows you to count the number of complete rows. To build a validation expression using this function, you can add an operator to return an error if the total equals zero.
count(Table)==0
Other simple operators can also be used to validate that the count is within the allowed limits:
==
count(Table)==0
Returns true (error) if the total equals zero
!=
count(Table)!=100
Returns true if the total is not equal to 100
>
count(Table)>100
Returns true if the total is greater than 100
<
count(Table)<10
Returns true if the total is less than 10
The sum() function adds all the numbers within the range of a column to calculate a total. By adding operators to this function, it can be turned into a validation expression that compares the total against a preset number and returns an error when the entered data does not match the expected value. For example, in a sales table where the "percentage" column represents the portion of revenue for each product type, the sum of each cell should total 100.
The following function will detect if the total is different from 100 and display an error message if this condition is met:
sum(SalesTable,PercentageColumn)!=100
The dateDifferenceInBusinessDays() function calculates the total number of business days (Monday through Friday) between two dates. Similar to count(), adding an operator to the calculation allows it to be compared against a minimum or maximum value. For example:
dateDifferenceInBusinessDays(RequestDate,DeliveryDate)<=5
This formula will count the days between the request date and the delivery date, excluding weekends, and display an error message if the condition is true, i.e., if the period between the two dates is less than or equal to 5 days.
Other highly useful expressions are those that allow you to compare data entered in different fields in terms of similarity or difference between their values.
The equal() function checks both values (whether text, numbers, or dates) and returns true if they are identical, displaying an error message and preventing the instance from being submitted.
Using the form you’ve designed as a base, create a new Number field with the label "Mobile Phone" and the name "mobile_phone" in the "Contact Information" section.
To ensure that the number entered in this field is different from the one entered in the "Phone" field, you can use this validation expression. Click the fx icon on the far right of the Expression field and write the formula:
equal(phone,mobile_phone)
Next, press Confirm. If this condition is met, an alert will be displayed, which we will define in the Message field, for example: "The landline and mobile phone numbers cannot be the same."
A simpler alternative to this function can be constructed using operators for the comparison. For example, if you replace the equal() formula from the previous example with the expression phone==mobile_phone, the same validation will be performed, and you will get the same results.
Let’s look at the different types of operators you can use to compare data from two fields, along with an example of each and the action:
==
Field1==Field2
FirstName==LastName
Checks if the fields are equal
!=
Field1!=Field2
Email!=ConfirmEmail
Checks if the fields are different
>
Field1>Field2
UnitsSold>Stock
Checks if the first field is greater than the second
<
Field1<Field2
Quantity<MinimumQuantity
Checks if the first field is less than the second
You can also combine the greater than (>) and less than (<) operators with the equal (=) operator for more specific results, e.g., RecommendedQuantity>=MaximumQuantity. In all these cases, meeting the condition will return the value true and display the error message entered in the Message property.
The smaller() function allows you to compare the dates entered in two fields and return an error message if the first is earlier than the second, for example, "The end date cannot be earlier than the start date." Its structure is as follows:
smaller(StartDate,EndDate)
Conversely, the larger() function checks if the first date is later than the second and returns an error message if this condition is met. Its structure is shown below:
larger(EndDate,StartDate)
There are variations for both expressions that also return an error message if the two dates are identical, i.e., applying the functionality of equal(). To do this, we need to modify the arguments of these functions as follows:
smallerEq (Date1,Date2): checks that the first date is earlier than or equal to the second
largerEq (Date1,Date2): checks that the first date is later than or equal to the second
These functions can also be used to compare other types of values, such as numbers or text strings.
Expressions based on AND, OR, and NOT operators allow validations based on the fulfillment of certain true/false conditions, either in the value of a field or expression (in the case of NOT) or in the result of two expressions applied to a field (AND and OR). Let’s explore each of these logical validations in detail.
The not() function returns true if the field has a negative value and is typically used for boolean fields, which act as a toggle that can be activated or deactivated based on a binary yes/no logic. We will explore this type of field in more detail later in this section.
If the boolean is deactivated, i.e., if the response is negative, the expression will return an error message. For example, it can be applied to a boolean field named "Confirmation" to display an error message "You must accept the terms and conditions to continue" if the following expression returns true:
not(Confirmation)
More complex validations can also be performed by applying the not() expression to the evaluation of other functions, for example:
not(equal(Field1,Field2))
If the condition that fields 1 and 2 are equal is not met, an error message will be displayed. Remember that the editor will display an error message if the number of open parentheses in the formula does not match the number of closed parentheses.
With the and() expression, you can check the simultaneous fulfillment of two different expressions and display an error message when this occurs (i.e., when both return true), using the following logical check:
false
false
false
true
false
false
false
true
false
true
true
true
For example, if you have a form with two boolean fields where at least one must be activated, such as requesting a physical receipt or a virtual receipt, the and() function can be applied to display an error if both checkboxes are deactivated.
and(not(PhysicalReceipt),not(VirtualReceipt))
Let’s analyze how this expression works:
false: The user has requested a physical receipt
false: The user has requested a virtual receipt
false: No error message is displayed, as neither condition is met
true: The user has not requested a physical receipt
false: The user has requested a virtual receipt
false: No error message is displayed, as only one condition is met
false: The user has requested a physical receipt
true: The user has not requested a virtual receipt
false: No error message is displayed, as only one condition is met
true: The user has not requested a physical receipt
true: The user has not requested a virtual receipt
true: The error message is displayed because both conditions are met
More complex structures can be built for this type of validation by nesting other formulas with the AND operator within the field function to control a greater number of expressions:
and(Expression1,and(Expression1,Expression2))
In this way, the evaluation of expression 1 will return a true or false value that will be compared with the total true or false value returned by the second and() function when checking expressions 2 and 3. These functions can continue to be nested as long as the same structure is maintained.
The or() expression allows you to verify that at least one of two expressions is met and display an error message unless both return false, using this logical check:
false
false
false
true
false
true
false
true
true
true
true
true