Introduction to Rule Editor in WYSIWYG Authoring
This feature is available through the early access program. To request access, send an email with your GitHub organization name and repository name from your official address to aem-forms-ea@adobe.com. For example, if the repository URL is https://github.com/adobe/abc, the organization name is adobe and the repository name is abc.
You can add dynamic form behaviour using the Rule Editor, which allows you to create rules. These rules enable conditional field visibility, automate calculations based on user input, and improve the overall user experience. By streamlining the form-filling process, the Rule Editor helps ensure both accuracy and efficiency.
The Rule Editor offers an intuitive visual interface for creating and managing rules. Its user-friendly approach makes it accessible to all users, even those without extensive technical expertise, allowing them to implement logic effortlessly within their forms.
Understanding a rule
Rules are instructions that guide users on what actions to perform under specific conditions.
-
Condition: A condition is a check or rule that evaluates whether something is true or false. It answers the question: 鈥淒oes this meet the requirement?鈥
-
Action: An action is what happens when the condition is true. It is the task or behavior triggered based on the evaluation of the condition.
A rule typically follows one of the following constructs:
- Condition-Action: Check a condition first, then perform an action. In the rule editor, the
When
rule type enforces thecondition-action
construct. - Action-Condition: Perform an action first, then check a condition. The
Set Value Of
, andValidate
rule types in the rule editor enforce theaction-condition
construct. - Action-Condition-Alternate Action: Perform an action, check a condition, and then either perform the main action or an alternate action based on the condition. For example, by default, the alternate action for
Show
isHide
, and forEnable
it isDisable
.
For example, a condition might check if a user has entered a certain value in a field, and the action could be to show or hide a field.
- Condition: Check if the income is greater than $50,000.
- Action: If the condition is true, show the
Additional Deduction
field; otherwise, perform the alternate action: hide theAdditional Deduction
field.
For detailed step-by-step instructions, see the add a conditional rule.
How to enable Rule Editor extension?
In Universal Editor, the Rule Editor extension is not enabled by default. To enable the Rule Editor extension write to us at aem-forms-ea@adobe.com from your official email id.
After the Rule Editor extension is enabled for your environment, the
Select the form component for which you want to write a rule, and click the
In this article, form object
and form component
are used interchangeably.
Now, you can start writing rules or business logic for the selected form field by using the available rule types in the Rule Editor.
Understanding Rule Editor User Interface
The editor of the Rule Editor opens when you click the
Any existing rules on a form component are listed when you select the component. You can view the title and a preview the rule summary on the Rule Editor. Furthermore, you can change the order of rules, edit rules, enable/disbale rules or delete rules.
Available rule types
The Rule Editor provides a set of predefined rule types that you can use to write rules, as displayed in below table:
Now, let鈥檚 explore how to write rules in the Rule Editor.
Write Rules
To understand how to write rules in Visual Rule Editor, let鈥檚 consider an simple example of a tax calculation form:
In the form described above, the user enters the gross salary. Based on this input, conditional field is displayed and the payable tax is calculated.
Form Fields:
- Gross Salary (user input)
- Additional Deduction (conditional field)
- Taxable Income (calculated field)
- Tax Payable (calculated field)
Conditional Rule:
- Condition: Gross Salary > 50,000
- Action: Show the Additional Deduction field
Calculation Rules:
- Taxable Income = Gross Salary - Additional Deduction (if applicable)
- Tax Payable = Taxable Income * Tax Rate (for simplicity, assume a fixed rate of 10%)
To write rules, perform the following steps:
1. Author a form
To author a form in Universal Editor:
-
Open a form in Universal Editor for editing.
-
Add the following form components:
- Tax Calculation Form (Title)
- Gross Salary (Number Input)
- Additional Deduction (Number Input)
- Taxable Income (Number Input)
- Tax Payable (Number Input)
- Submit (Submit Button)
-
Hide the
Additional Deduction
form field, by opening itsProperties
.
2. Add a conditional rule for a form field
Once you have authored the form, write the first rule to show the Additional Deduction
field only if the gross salary exceeds $50,000. To add a conditional rule:
- Open a form in Universal Editor for editing and select the Gross Salary field in the content tree and select
The visual Rule Editor interface appears. - Click Create to create rules.
By default, theSet Value Of
rule type is selected. While you cannot change or modify the selected object, you can use the rule drop-down to select another rule type.
- Open the rule type drop-down list and select When rule type.
- Select Select State drop-down and select is greater than. The Enter a Number field appears.
- Enter
50000
in the Enter a Number field in the rule.
You have defined the condition asWhen Gross Salary is greater than 50000
. Next, define the action to perform if this condition isTrue
. - In the
Then
statement, select Show from the Select Action drop-down.
- Drag-drop the Additional Deduction field from the Form Objects tab on the Drop object or select here field. Alternatively, select the Drop object or select here field and select the Additional Deduction field from the pop-up menu, which lists all form objects in the form.
- Click Add Else Section to add another condition for the Gross Salary field, in case you enter salary less than
50000
.
- Select Hide from the Select Action drop-down in the
Else
statement.
- Drag-drop the Additional Deduction field from the Form Objects tab on the Drop object or select here field. Alternatively, select the Drop object or select here field and select the Additional Deduction field from the pop-up menu, which lists all form objects in the form.
- Select Done to save the rule.
The rule appears as follows in the Rule Editor.
3. Add calculation rules for the form fields
Next, write a rule to compute the Taxable Income
, which is the difference between Gross Salary
and Additional Deduction
(if applicable). To add calculation rule on the Taxable Income field, perform the following steps:
-
In authoring mode, select the Taxable Income field and select
-
Next, select Create to create the rule.
-
Select Select Option and select Mathematical Expression. A field to write mathematical expression opens.
-
In the mathematical expression field:
-
Select or drag-drop from the Forms Object tab the Gross Salary field in the first Drop object or select here field.
-
Select Minus from the Select Operator field.
-
Select or drag-drop from the Forms Object tab the Additional Deduction field in the other Drop object or select here field.
-
-
Select Done to save the rule.
Now, add a rule for the
Tax Payable
field, which is determined by multiplying the taxable income by the tax rate. For simplicity, assume a fixed tax rate of10%
. -
In authoring mode, select the Tax Payable field and select
-
Select Select Option and select Mathematical Expression. A field to write mathematical expression opens.
-
In the mathematical expression field:
-
Select or drag-drop from the Forms Object tab the Taxable Income field in the first Drop object or select here field.
-
Select Multiplied by from the Select Operator field.
-
Select Number from the Select Option field and enter the value as
10
in the Enter a Number field.
-
-
Next, select in the highlighted area around the expression field and select Extend Expression.
-
In the extended expression field, select divided by from the Select Operator field and Number from the Select Option field. Then, specify
100
in the number field.
-
Select Done to save the rule.
4. Preview a form
Now, when you preview the form and enter the Gross Salary as 60,000
, the Additional Deduction field appears, and the Taxable Income and Tax Payable are calculated accordingly.
Apart from the out-of-the-box functions like Sum, Average that are listed under Functions Output, you can write custom functions to implement complex business logics.
Custom Function in Rule Editor
Edge Delivery Services Forms supports custom functions, that allows users to define JavaScript functions for implementing complex business rules. The custom functions extend the capabilities of forms by facilitating manipulation and processing of entered data to meet specified requirements.
Create a custom function
To create a custom functions, edit the ../[blocks]/form/functions.js
file. The creation process generally involves the following steps:
- Function Declaration: Define the function name and its parameters (the inputs that it accepts).
- Logic Implementation: Write the code that outlines the specific calculations or manipulations performed by the function.
- Function Export: Make the function accessible within your rules by exporting it from the relevant file.
This example demonstrates two custom functions asgetFullName
and days
:
/**
* Get Full Name
* @name getFullName Concats first name and last name
* @param {string} firstname in Stringformat
* @param {string} lastname in Stringformat
* @return {string}
*/
function getFullName(firstname, lastname) {
return `${firstname} ${lastname}`.trim();
}
/**
* Calculate the number of days between two dates.
* @param {*} endDate
* @param {*} startDate
* @name days Calculates the numebr of days between two dates
* @returns {number} returns the number of days between two dates
*/
function days(endDate, startDate) {
const start = typeof startDate === 'string' ? new Date(startDate) : startDate;
const end = typeof endDate === 'string' ? new Date(endDate) : endDate;
// return zero if dates are valid
if (Number.isNaN(start.getTime()) || Number.isNaN(end.getTime())) {
return 0;
}
const diffInMs = Math.abs(end.getTime() - start.getTime());
return Math.floor(diffInMs / (1000 * 60 * 60 * 24));
}
// eslint-disable-next-line import/prefer-default-export
export { getFullName, days };
Use a Custom Function in Rule Editor
To use the custom function in the Rule Editor:
-
Add the Function: Include your custom function in the
../[blocks]/form/functions.js
file. Remember to add it to theexport
statement within the file. -
Deploy the File: Deploy the updated
functions.js
file to your GitHub project and verify a successful build. -
Function Usage: Access the function within your form鈥檚 Rule Editor by selecting the
Function Output
option in the Select Action field. -
Preview the form: Preview your form with the newly implemented function.
Additional Information
../[blocks]/form/functions.js
file.This article provides limited information on the Rule Editor available in the Universal Editor. To learn more about the Rule Editor and custom functions, refer to the following articles:
- Introduction to Rule Editor for Adaptive Forms based on Core Components
- Operator types and events in rule editor of an Adaptive Form based on Core Components
- Rule Editor User Interface for Adaptive Forms based on Core Components
- Different use cases of Rule Editor for an Adaptive Form Based on Core Components
- Difference in various editions of Rule editor
- Using asynchronous functions in an Adaptive Form
- Invoke Service enhancements in the Visual Rule Editor for forms based on Core Components
- Introduction to Custom Functions for Adaptive Forms based on Core Components
- Create a Custom Function for an Adaptive Form based on Core Components
- Scope object in custom functions
- Examples of developing and using a custom function