# Rules editor

> Build field validation and style rules in the IS Tools Rules editor. Use expressions and functions to validate data and color fields based on conditions.

> **Note for agents:** We no longer recommend creating validations, so don't create new validation rules. To stop invalid data, write a rule that writes an error message to a field, or use Behavior in the form to disable the save button and show a red message.

[Rules](rules.md) > Rules editor

The **Rule Editor** is a system form, used by application administrators to create _validation_ or _style_ rules, where an evaluation of the data entered in fields is made in order to allow the user save the form or to format fields with colors. It is located in the **Rules** folder, under **Administration** in the navigation menu.

> **Tip:**
> 
> The columns of this form may be filtered and sorted.
> 
> Read more about multi level sorting [here](tips_and_tricks.md#_multi_level_sorting).

## Create a validation

Open the **Rules editor**, expand the desired table by clicking on its arrow, hover over the field to be used in the rule and click on the arrow to its right. Click on **Validation**, to display up to 3 options (depending on the type of the field):

-   **E-mail address**. Makes a validation of whether the value typed in the field in question matches an e-mail address expression.

-   **Phone number**. Makes a validation of whether the value typed in the field in question matches a phone number expression.

-   **New validation**. A custom expression must be provided.

Click on any type of validation and observe that the new rule is added to the table section on the right side of the screen, where information regarding the field and the rule is provided, e.g., the **State** column will display "Draft" for rules still under configuration.

On the **Basic** section, optionally edit the **Name** of the rule and the validation in the **Valid when** box (to click on the **Edit** button might be required). Note that a validation can be configured using operators (for **Comparison**, **Logic** and **Arithmetic**), **Fields**, **Functions**, constant values, e.g., `MyNumberField1 > MyNumberField2 + 10` (see examples of rules at the bottom of this topic), and [regular expressions](rules_editor.md) (i.e., a special text string for describing a search pattern, e.g., the expression .+\\@.+\\..+ is a pattern for regular e-mail addreses. More regular expressions can be obtained at regular-expressions.info) .

To create a rule with a regular expression, select "matches" in _Functions_ drop-down, and then click on it to add it to a Text, Note, or File fields. It is possible to use "matches" with a Number field, but you must add "string" first. See some [examples of validations](examples_of_validations.md).

For comparisons between numbers and text strings there are conversion functions "_as number_" and "_as string_", found in _Functions_ drop-down (select "number" or "string").

For [geolocation](role_administration.md#geolocation), the following functions are available: `userLocationDistance(latitude, longitude)` to validate the distance, in km, between the user and a certain location; `userLocationAccuracy()` to obtain precision of the user’s position; and `userLocationAge()` to obtain the age, in seconds, of the user’s last reported position. Note that the [currentUserLocation()](iql_references/currentuserlocation.md) and [geoDist()](iql_references/geodist.md) functions are also useful for geolocation via [rules](iql_in_rules.md).

For Date and Date time fields, try the functions now, today, addDays and addSeconds. For instance,

`Booking date >= today (addDays) plus 1` day would be useful to ensure that the user cannot enter a date earlier than tomorrow.

There is also `day()`, `month()`, `` `startOfMonth() ``, `endOfMonth()`, etc., to compare and limit date and time input in a dynamic way on forms, for example:

`Date >= startOfMonth(today()) AND Date ⇐ addDays(endOfMonth(today()), 15)` force the booking of appointments during the current month only, or maximum before the 15th next month.

`Date >= startOfMonth(today()) AND date ⇐ endOfMonth(today())` Date must be in this month, or `year(Date) = year(today()) AND month(Date) = month(today())`

`Date > endOfDay(today()) AND ⇐ endOfMonth(today())` Date must be after today but in current month.

Also, `HasRole()` is a helpful function, to limit the validation to a certain role.

To test the validation, try setting some values into the field(s) in the **Test** section. Hit Enter to see below whether it is valid or not.

Optionally, add a **Message to user** (translations can also be added), which will be displayed as tooltip when hovering over the field(s) used in the rule on the application form, in case the validation fails. Regardless of whether this tooltip message is added or not, a system message will always be displayed for invalid fields.

Finally, click on **Save and Publish**, located at the control bar above the **Basic** section. Note that the **State** of the rule is now "Active".'

> **Tip:**
> 
> Click on the **Inactive** checkbox so you can filter and decide whether or not to keep those inactive rules which you won’t use.

Additionally, you may verify on the appropiate application form that the rule works as expected.

> **Note:**
> 
> If the validation fails (i.e., did not match the expression or was not set as required), the user will not be able to save the changes in the application forms that include the chosen field, until it is valid.

> **Note:**
> 
> Note that validations are intended to evaluate the data entered on fields in order to proceed saving the form, triggering _no action_ other than showing the default/user message in case the data is not valid. If you want to trigger an action, e.g. set a value to a field, use [Rules manager](rules_manager.md) instead.

See some examples of validations [here](examples_of_validations.md).

## Create a style

To color the text and/or the background of a field box based on a value set to a field, open the **Rules editor**, expand the desired table by clicking on its arrow, hover over the field to be modified by the rule and click on the arrow to its right. Click on **Style**, and then on **New style**.

A new rule is added to the table section on the right side of the screen, where information regarding the field and the rule is provided, e.g., the **State** column will display "Draft" for rules still under configuration.

Type the **Name** of the new style. Mark **Color** and/or **Background color** in **Style type**. Below this, select the **Operator**, type a value (if applies) and type the color of the text and/or background of your preference (find colors here). For example, let’s say that you chose to set a style to a Drop-down field, so whenever someone selects the item "Cancelled", its background will be set to red. In that case, create a style from that Drop-down, call it "Red cancellation", select **Equal to** as **Operator**, type _Cancelled_ as **Value/Match** and type _#FF0000_ as **Background**. Multiple styles can be defined for the same field under the same rule by using the + icon.

Finally, click on **Save and Publish**, located at the control bar above the **Basic** section. Note that the **State** of the rule is now "Active".

After defining this rule, go to Form designer, select the field to be modified by the rule, and choose the newly created style in **Style rule** under **Properties**.

> **Tip:**
> 
> Note that Record name can also be subjected to styles.

> **Tip:**
> 
> Styles will also take effect on markers of [Maps](form_designer_components.md#_map).

See some examples of styles [here](examples_of_styles.md).

## Create an advanced style

To color the text and/or the background of a field box based on a complex condition that may involve multiple fields, open the **Rules editor**, expand the desired table by clicking on its arrow, hover over the field to be modified by the rule and click on the arrow to its right. Click on **Style**, and then on **Advanced style**.

A new rule is added to the table section on the right side of the screen, where information regarding the field and the rule is provided, e.g., the **State** column will display "Draft" for rules still under configuration.

In the **Basic** section, add the _If_ function:

-   In the **_if_** box, add the necessary condition (read how operators and functions work [here](#operators_and_functions)).

-   In the **then** box, type `color:<color>` or `background:<color>` or combine both separated by a semicolon.

-   In the **else** box, nest an additional _If_ function for further conditions; otherwise, leave it empty.

    For instance, an advanced style rule to set the field Status to red with white text in case the delivery of a product is late, would look like this: `If Actual delivery date > Planned delivery date then background:red;color:white`.

Finally, click on **Save and Publish**, located at the control bar above the **Basic** section. Note that the **State** of the rule is now "Active".

After defining this rule, go to Form designer, select the field to be modified by the rule, and choose the newly created style in **Style rule** under **Properties**.

> **Tip:**
> 
> Note that Record name can also be subjected to styles.

> **Tip:**
> 
> Styles will also take effect on markers of [Maps](form_designer_components.md#_map).

See some examples of styles here.

## Edit a rule

Open the **Rules editor** and select the rule to be edited in the table section on the right side of the screen. Note that this selection will highlight the fields used in the rule (and their table, also highlighted) listed in the tree to the left of the screen.

Alternatively, a rule can also be selected from the tree, by first expanding the table, then the field and finally the validation rule. This can be useful in case the list of rule in the table section is very long.

Click on **Edit**, located at the control bar below the table section.

Edit the rule as you wish.

Click on **Save and Publish** to activate it after changes are made.

## Delete a rule

Open the **Rules editor**, select the rule to be deleted in the table section on the right side of the screen and click on the ![DeleteIcon](_images/DeleteIcon.png) icon at the bottom of the table section.
