Custom functions in Adaptive Forms (Core Components)
Introduction
AEM Forms supports custom functions, allowing users to define JavaScript functions for implementing complex business rules. These custom functions extend the capabilities of forms by facilitating manipulation and processing of entered data to meet specified requirements. They also enable dynamic alteration of form behavior based on predefined criteria.
Uses of custom functions uses-of-custom-function
Advantages of using custom functions in Adaptive Forms are:
- Processing of data: Custom functions help process data entered into the forms fields.
- Validation of data: Custom functions enable you to perform custom checks on form inputs and provide specified error messages.
- Dynamic behavior: Custom functions allow you to control the dynamic behavior of your forms based on specific conditions. For example, you can show/hide fields, modify field values, or adjust form logic dynamically.
- Integration: You can use custom functions to integrate with external APIs or services. It helps in fetching data from external sources, sending data to external Rest endpoints, or performing custom actions based on external events.
Custom functions are essentially client libraries that are added in the JavaScript file. Once you create a custom function, it becomes available in the rule editor for selection by the user in an Adaptive Form. The custom functions are identified by the JavaScript annotations in the rule editor.
Supported JavaScript annotations for custom function js-annotations
JavaScript annotations are used to provide metadata for JavaScript code. It includes comments that start with specific symbols, for example, /** and @. The annotations provide important information about functions, variables, and other elements in the code. Adaptive Form supports the following JavaScript annotations for custom functions:
Name
The name is used to identify the custom function in the rule editor of an Adaptive Form. The following syntaxes are used to name a custom function:
@name [functionName] <Function Name>
@function [functionName] <Function Name>
@func [functionName] <Function Name>
.functionName
is the name of the function. Spaces are not allowed.<Function Name>
is the display name of the function in the rule editor of an Adaptive Form.
If the function name is identical to the name of the function itself, you can omit[functionName]
from the syntax.
Parameter
The parameter is a list of arguments used by custom functions. A function can support multiple parameters. The following syntaxes are used to define a parameter in a custom function:
-
@param {type} name <Parameter Description>
-
@argument
{type} name <Parameter Description>
-
@arg
{type}
name <Parameter Description>
.{type}
represents the parameter type. The allowed parameter types are:- string: Represents a single string value.
- number: Represents a single numeric value.
- boolean: Represents a single boolean value (true or false).
- string[]: Represents an array of string values.
- number[]: Represents an array of numeric values.
- boolean[]: Represents an array of boolean values.
- date: Represents a single date value.
- date[]: Represents an array of date values.
- array: Represents a generic array containing values of various types.
- object: Represents form object passed to a custom function instead of passing its value directly.
- scope: Represents the globals object, which contains read-only variables such as form instances, target field instances, and methods for performing form modifications within custom functions. It is declared as the last parameter in JavaScript annotations and is not visible in the rule editor of an Adaptive Form. The scope parameter accesses the object of the form or component to trigger the rule or event required for form processing. For further information on the Globals object and how to use it, click here.
The parameter type is not case-sensitive and spaces are not allowed in the parameter name.
<Parameter Description>
contains details about the purpose of the parameter. It can have multiple words.
Optional Parameters
By default, all parameters are mandatory. You can define a parameter as optional by either adding =
after the parameter type or enclosing the parameter name in []
. Parameters defined as optional in JavaScript annotations are displayed as optional in the rule editor.
To define a variable as an optional parameter, you can use any of the following syntaxes:
@param {type=} Input1
In the above line of code, Input1
is an optional parameter without any default value. To declare optional parameter with default value:@param {string=<value>} input1
input1
as an optional parameter with the default value set to value
.
@param {type} [Input1]
In the above line of code, Input1
is an optional parameter without any default value. To declare optional parameter with default value:@param {array} [input1=<value>]
input1
is an optional parameter of array type with the default value set to value
.
Ensure that the parameter type is enclosed in curly brackets {} and the parameter name is enclosed in square brackets.
Consider the following code snippet, where input2 is defined as an optional parameter:
/**
* optional parameter function
* @name OptionalParameterFunction
* @param {string} input1
* @param {string=} input2
* @return {string}
*/
function OptionalParameterFunction(input1, input2) {
let result = "Result: ";
result += input1;
if (input2 !== null) {
result += " " + input2;
}
return result;
}
The following illustration displays using the OptionalParameterFunction
custom function in the rule editor:
You can save the rule without specifying a value for the required parameters, but the rule is not executed and displays a warning message as:
When the user leaves the optional parameter empty, then the “Undefined” value is passed to the custom function for the optional parameter.
To learn more about how to define optional parameters in JSDocs, click here.
Return Type
The return type specifies the type of value that the custom function returns after execution. The following syntaxes are used to define a return type in a custom function:
-
@return {type}
-
@returns {type}
{type}
represents the return type of the function. The allowed return types are:- string: Represents a single string value.
- number: Represents a single numeric value.
- boolean: Represents a single boolean value (true or false).
- string[]: Represents an array of string values.
- number[]: Represents an array of numeric values.
- boolean[]: Represents an array of boolean values.
- date: Represents a single date value.
- date[]: Represents an array of date values.
- array: Represents a generic array containing values of various types.
- object: Represents form object instead of its value directly.
The return type is not case-sensitive.
Private
The custom function declared as private, does not appear in the list of custom functions in the rule editor of an Adaptive form. By default, custom functions are public. The syntax to declare custom function as private is @private
.
Guidelines while creating custom functions
To list the custom functions in the rule editor, you can use any one of the following formats:
Function statement with or without jsdoc comments
You can create a custom function with or without jsdoc comments.
function functionName(parameters)
{
// code to be executed
}
If the user does not add any JavaScript annotations to the custom function, it is listed in the rule editor by its function name. However, it is recommended to include JavaScript annotations for improved readability of the custom functions.
Arrow function with mandatory JavaScript annotations or comment
You can create a custom function with an arrow function syntax:
/**
* test function
* @name testFunction
* @param {string} a parameter description
* @param {string=} b parameter description
* @return {string}
*/
testFunction = (a, b) => {
return a + b;
};
/** */
testFunction1=(a) => (return a)
/** */
testFunction2 = a => a + 100;
If the user does not add any JavaScript annotations to the custom function, the custom function is not listed in the rule editor of an Adaptive Form.
Function expression with mandatory JavaScript annotations or comment
To list custom functions in the rule editor of an Adaptive Form, create custom functions in the following format:
/**
* test function
* @name testFunction
* @param {string} input1 parameter description
* @param {string=} input2 parameter description
* @return {string}
*/
testFunction = function(input1,input2)
{
// code to be executed
}
If the user does not add any JavaScript annotations to the custom function, the custom function is not listed in the rule editor of an Adaptive Form.
Create a custom function create-custom-function
Create a client library to call custom functions in the rule editor. For more information, see Using Client-Side Libraries.
Steps to create custom functions are:
Prerequisites to create a custom function
Before you begin adding a custom function to your Adaptive Forms, ensure you have the following:
Software:
-
Plain Text Editor (IDE): While any plain text editor can work, an Integrated Development Environment (IDE) like Microsoft Visual Studio Code offers advanced features for easier editing.
-
Git: This version control system is required for managing code changes. If you do not have it installed, download it from https://git-scm.com.
Create a client library create-client-library
You can add custom functions by adding a client library. To create a client library, perform the following steps:
Clone the Repository
Clone your AEM Forms as a Cloud Service Repository:
-
Open your command line or terminal window.
-
Navigate to the desired location on your machine where you want to store the repository.
-
Run the following command to clone the repository:
git clone [Git Repository URL]
This command downloads the repository and creates a local folder of the cloned repository on your machine. Throughout this guide, we refer to this folder as the [AEMaaCS project directory].
Add a Client Library Folder
To add new client library folder to the [AEMaaCS project directory], follow these steps:
-
Open the [AEMaaCS project directory] in an editor.
-
Locate
ui.apps
. -
Add new folder. For example, add a folder named as
experience-league
. -
Navigate to
/experience-league/
folder and add aClientLibraryFolder
. For example, create a client library folder named ascustomclientlibs
.Location is: [AEMaaCS project directory]/ui.apps/src/main/content/jcr_root/apps/
Add files and folders to the Client Library folder
Add the following to the added client library folder:
- .content.xml file
- js.txt file
- js folder
Location is: [AEMaaCS project directory]/ui.apps/src/main/content/jcr_root/apps/experience-league/customclientlibs/
-
In the
.content.xml
add the following lines of code:code language-javascript <?xml version="1.0" encoding="UTF-8"?> <jcr:root xmlns:cq="http://www.day.com/jcr/cq/1.0" xmlns:jcr="http://www.jcp.org/jcr/1.0" jcr:primaryType="cq:ClientLibraryFolder" categories="[customfunctionscategory]"/>
note note NOTE You can choose any name for client library folder
andcategories
property. -
In the
js.txt
add the following lines of code:code language-javascript #base=js function.js
-
In the
js
folder, add the javascript file asfunction.js
which includes the custom functions:code language-javascript /** * Calculates Age * @name calculateAge * @param {object} field * @return {string} */ function calculateAge(field) { var dob = new Date(field); var now = new Date(); var age = now.getFullYear() - dob.getFullYear(); var monthDiff = now.getMonth() - dob.getMonth(); if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < dob.getDate())) { age--; } return age; }
-
Save the files.
Include the new folder in filter.xml:
-
Navigate to the
/ui.apps/src/main/content/META-INF/vault/filter.xml
file in your [AEMaaCS project directory]. -
Open the file and add the following line at the end:
<filter root="/apps/experience-league" />
-
Save the file.
Deploy the newly created Client library folder to your AEM environment
Deploy the AEM as a Cloud Service, [AEMaaCS project directory], to your Cloud Service environment. To deploy to your Cloud Service environment:
-
Commit the changes
- Add, commit, and push the changes in the repository using the below commands:
code language-javascript git add . git commit -a -m "Adding custom functions" git push
-
Deploy the updated code:
- Trigger a deployment of your code through the existing full-stack pipeline. This automatically builds and deploys the updated code.
If you haven’t already set up a pipeline, refer to the guide on how to set up a pipeline for AEM Forms as a Cloud Service.
Once the pipeline is executed successfully, the custom function added in the client library becomes available in your Adaptive Form rule editor.
Add client library to an Adaptive Form use-custom-function
Once you have deployed your client library to your Forms CS environment, use its capabilities in your Adaptive Form. To add the client library in your Adaptive Form
-
Open your form in edit mode. To open a form in edit mode, select a form and select Edit.
-
Open the Content browser, and select the Guide Container component of your Adaptive Form.
-
Click the Guide Container properties
-
Open the Basic tab and select the name of the client library category from the drop-down list (in this case, select
customfunctionscategory
).note note NOTE Multiple categories can be added by specifying a comma-separated list within the Client library category field. -
Click Done.
You can use the custom function in the rule editor of an Adaptive Form using the JavaScript annotations.
Using a custom function in an Adaptive Form
In an Adaptive Form, you can use custom functions within the rule editor. Let us add the following code to the JavaScript file (Function.js
file) to calculate age based on the Date of Birth(YYYY-MM-DD). Create a custom function as calculateAge()
which takes the date of birth as input and returns age:
/**
* Calculates Age
* @name calculateAge
* @param {object} field
* @return {string}
*/
function calculateAge(field) {
var dob = new Date(field);
var now = new Date();
var age = now.getFullYear() - dob.getFullYear();
var monthDiff = now.getMonth() - dob.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && now.getDate() < dob.getDate())) {
age--;
}
return age;
}
In the above example, when the user enters the date of birth in the format (YYYY-MM-DD), the custom function calculateAge
is invoked and returns the age.
Let’s preview the form to observe how the custom functions are implemented through the rule editor:
Set the dropdown list options using custom functions
Rule Editor in Core Components does not support the Set Options of property to set the dropdown list options at runtime. However, you can set the dropdown list options using custom functions.
Look at the code below to see how we can set the dropdown list options using custom functions:
/**
* @name setEnums
* @returns {string[]}
**/
function setEnums() {
return ["0","1","2","3","4","5","6"];
}
/**
* @name setEnumNames
* @returns {string[]}
**/
function setEnumNames() {
return ["Sunday","Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
}
In the above code, setEnums
is used to set the enum
property and setEnumNames
is used to set the enumNames
property of dropdown.
Let’s create a rule for the Next
button, which sets the value of dropdown list option when the user clicks the Next
button:
Refer to the illustration below to demonstrate where the options of the dropdown list are set upon clicking the Display button:
Support for asynchronous functions in custom functions support-of-async-functions
Asynchronous custom functions do not appear in the rule editor list. However, it is possible to invoke asynchronous functions within custom functions created using synchronous function expressions.
Look at the code below to see how we can invoke asynchronous functions using custom functions:
async function asyncFunction() {
const response = await fetch('https://petstore.swagger.io/v2/store/inventory');
const data = await response.json();
return data;
}
/**
* callAsyncFunction
* @name callAsyncFunction callAsyncFunction
*/
function callAsyncFunction() {
asyncFunction()
.then(responseData => {
console.log('Response data:', responseData);
})
.catch(error => {
console.error('Error:', error);
});
}
In the above example, the asyncFunction function is an asynchronous function
. It performs an asynchronous operation by making a GET
request to https://petstore.swagger.io/v2/store/inventory
. It waits for the response using await
, parses the response body as JSON using the response.json()
, and then returns the data. The callAsyncFunction
function is a synchronous custom function that invokes the asyncFunction
function and displays the response data in the console. Although the callAsyncFunction
function is synchronous, it calls the asynchronous asyncFunction function and handles its result with then
and catch
statements.
To see its working, let us add a button and create a rule for the button that invokes the asynchronous function upon a button click.
Refer to the illustration of the console window below to demonstrate that when the user clicks the Fetch
button, the custom function callAsyncFunction
is invoked, which in turn calls an asynchronous function asyncFunction
. Inspect the console window to view the response to the button click:
Let’s dive into the features of custom functions.
Various features for Custom Functions
You can use custom functions to add personalized features to forms. These functions support various abilities such as working with specific fields, using global fields, or caching. This flexibility allows you to customize forms according to your organization’s requirements.
Field and Global scope objects in custom functions support-field-and-global-objects
Field objects refer to the individual components or elements within a form, such as text fields, checkboxes. The Globals object contains read-only variables such as form instance, target field instance and methods to do form modifications within custom functions.
param {scope} globals
has to be the last parameter and it is not displayed in the rule editor of an Adaptive Form.Let’s learn how custom functions use field and global objects with the help of a Contact Us
form using different use cases.
SetProperty
ruleAdd the following code in the custom function as explained in the create-custom-functionsection, to set the form field as Required
.
code language-javascript |
---|
|
note note |
---|
NOTE |
|
In this example, validation of the personaldetails
panel occurs upon clicking the button. If no errors are detected in the panel, another panel, the feedback
panel, becomes visible upon button click.
Let’s create a rule for the Next
button, which validates the personaldetails
panel and makes the feedback
panel visible when the user clicks the Next
button.
Refer to the illustration below to demonstrate where the personaldetails
panel is validated upon clicking the Next
button. In case all the fields within the personaldetails
are validated, the feedback
panel becomes visible.
If errors are present in the fields of the personaldetails
panel, they are displayed at the field level upon clicking the Next
button, and the feedback
panel remains invisible.
Add the following code in the custom function as explained in the create-custom-functionsection, to validate the field.
code language-javascript |
---|
|
note note |
---|
NOTE |
If no argument is passed in the validate() function, it validates the form. |
In this example, a custom validation pattern is applied to the contact
field. Users are required to input a phone number starting with 10
followed by 8
digits. If the user enters a phone number that does not start with 10
or contains more or less than 8
digits, a validation error message appears upon the button click:
Now, next step is to create a rule for the Next
button that validates the contact
field on the button click.
Refer to the illustration below to demonstrate that if the user enters a phone number that does not start with 10
, an error message appears at the field level:
If the user enters a valid phone number and all fields in the personaldetails
panel are validated, the feedback
panel appears on the screen:
Add the following code in the custom function as explained in the create-custom-functionsection, to reset the panel.
code language-javascript |
---|
|
note note |
---|
NOTE |
If no argument is passed in the reset() function, it validates the form. |
In this example, the personaldetails
panel resets upon clicking the Clear
button. Next step is to create a rule for the Clear
button that resets the panel on the button click.
See the illustration below to display that if the user clicks the clear
button, the personaldetails
panel resets:
You can use the markFieldAsInvalid()
function to define a field as invalid and set custom error message at a field level. The fieldIdentifier
value can be fieldId
, or field qualifiedName
, or field dataRef
. The value of the object named option
can be {useId: true}
, {useQualifiedName: true}
, or {useDataRef: true}
.
The syntaxes used to mark a field as invalid and set a custom message are:
globals.functions.markFieldAsInvalid(field.$id,"[custom message]",{useId: true});
globals.functions.markFieldAsInvalid(field.$qualifiedName, "[custom message]", {useQualifiedName: true});
globals.functions.markFieldAsInvalid(field.$dataRef, "[custom message]", {useDataRef: true});
Add the following code in the custom function as explained in the create-custom-functionsection, to enable a custom message at the field level.
code language-javascript |
---|
|
In this example, if the user enters less than 15 characters in the comments textbox, a custom message appears at the field level.
Next step is to create a rule for the comments
field:
See the demonstration below to display that entering negative feedback in the comments
field triggers the display of a custom message at the field level:
If the user enters more than 15 characters in the comments textbox, the field gets validated and the form is submitted:
The following line of code:globals.functions.submitForm(globals.functions.exportData(), false);
is used to submit the form data after manipulation.
- The first argument is the data to be submitted.
- The second argument represents whether the form is to be validated before submission. It is
optional
and set astrue
by default. - The third argument is the
contentType
of the submission, which is also optional with the default value asmultipart/form-data
. The other values can beapplication/json
andapplication/x-www-form-urlencoded
.
Add the following code in the custom function as explained in the create-custom-functionsection, to submit the manipulated data at the server:
code language-javascript |
---|
|
In this example, if the user leaves the comments
textbox empty, the NA
is submitted to the server at form submission.
Now, create a rule for the Submit
button that submits data:
Refer to the illustration of the console window
below to demonstrate that if the user leaves the comments
textbox empty, then the value as NA
is submitted at the server:
You can also inspect the console window to view the data submitted to the server:
Add the following line of code as explained in the create-custom-functionsection, to customize the submission or failure message for form submissions and display the form submission messages in a modal box:
code language-javascript |
---|
|
In this example, when the user uses the customSubmitSuccessHandler
and customSubmitErrorHandler
custom functions, the success and failure messages are displayed in a modal. The JavaScript function showModal(type, message)
is used to dynamically create and display a modal dialog box on a screen.
Now, create a rule for successful form submission:
Refer to the illustration below to demonstrate that when the form is submitted successfully, the success message is displayed in a modal:
Similarly, let us create a rule for failed form submissions:
Refer to the illustration below to demonstrate that when the form submission fails, the error message is displayed in a modal:
To display form submission success and failure in a default manner, the Default submit Form Success Handler
and Default submit Form Error Handler
functions are available out of the box.
In case, the custom submission handler fails to perform as expected in existing AEM Projects or forms, refer to the troubleshooting section.
Rules created using the visual rule editor on a repeatable panel apply to the last instance of the repeatable panel. To write a rule for a specific instance of the repeatable panel, we can use a custom function.
Let us create another form to collect information about travelers heading to a destination. A traveler panel is added as a repeatable panel, where the user can add details for 5 travelers using the Add Traveler
button.
Add the following line of code as explained in the create-custom-function section, to perform actions in a specific instance of the repeatable panel, other than the last one:
code language-javascript |
---|
|
In this example, the hidePanelInRepeatablePanel
custom function performs an action in a specific instance of the repeatable panel. In the above code, travelerinfo
represents the repeatable panel. The repeatablePanel[1].traveler, {visible: false}
code hides the panel in the second instance of the repeatable panel.
Let us add a button labeled Hide
and add a rule to hide the second instance of a repeatable panel.
Refer to the video below to demonstrate that when the Hide
is clicked, the panel in the second repeatable instance hides:
embed |
---|
|
Add the following line of code, as explained in the create-custom-function section, to load the pre-filled value in a field when the form is initialized:
code language-javascript |
---|
|
In the aforementioned code, the testImportData
function prefills the Booking Amount
textbox field when the form loads. Let us assume that the booking form requires the minimum booking amount to be 10,000
.
Let’s create a rule at form initialization, where the value in the Booking Amount
textbox field is prefilled with a specified value when the form loads:
Refer to the screenshot below, which demonstrates that when the form loads, the value in the Booking Amount
textbox is pre-filled with a specified value:
Add the following line of code, as explained in the create-custom-function section, to set focus on the specified field when the Submit
button is clicked.:
code language-javascript |
---|
|
Let us add a rule to the Submit
button to set focus on the Email ID
textbox field when it is clicked:
Refer to the screenshot below, which demonstrates that when the Submit
button is clicked, the focus is set on the Email ID
field:
note note |
---|
NOTE |
You can use the optional $focusOption parameter, if you want to focus on the next or previous field relative to the email field. |
dispatchEvent
propertyAdd the following line of code, as explained in the create-custom-function section, to add a panel when the Add Traveler
button is clicked using the dispatchEvent
property:
code language-javascript |
---|
|
Let us add a rule to the Add Traveler
button to add the repeatable panel when it is clicked:
Refer to the gif below, which demonstrates that when the Add Traveler
button is clicked, the panel is added using the dispatchEvent
property:
Similarly, add the following line of code, as explained in the create-custom-function section, to delete a panel when the Delete Traveler
button is clicked using the dispatchEvent
property:
code language-javascript |
---|
|
Let us add a rule to the Delete Traveler
button to delete the repeatable panel when it is clicked:
Refer to the gif below, which demonstrates that when the Delete Traveler
button is clicked, the traveler panel is deleted using the dispatchEvent
property:
Caching support for custom function
Adaptive Forms implement caching for custom functions to enhance response time while retrieving the custom function list in the rule editor. A message as Fetched following custom functions list from cache
appears in the error.log
file.
In case the custom functions are modified, the caching becomes invalidated, and it is parsed.
Troubleshooting troubleshooting
-
If the custom submission handler fails to perform as expected in existing AEM Projects or forms, perform the following steps:
-
Ensure that the core components version is updated to 3.0.18 and later. However, for existing AEM Projects and forms, there are additional steps to follow:
-
For the AEM project, the user should replace all instances of
submitForm('custom:submitSuccess', 'custom:submitError')
withsubmitForm()
and deploy the project through the Cloud Manager pipeline. -
For existing forms, if the custom submission handlers are not functioning correctly, the user needs to open and save the
submitForm
rule on the Submit button using the Rule Editor. This action replaces the existing rule fromsubmitForm('custom:submitSuccess', 'custom:submitError')
withsubmitForm()
in the form.
-
-
If the JavaScript file containing code for custom functions has an error, the custom functions are not listed in the rule editor of an Adaptive Form. To check the custom function list, you can navigate to the
error.log
file for the error. In case of an error, the custom function list appears empty:In case of there is no error, the custom function are fetched and appear in the
error.log
file. A message asFetched following custom functions list
appears in theerror.log
file:
Considerations
-
The
parameter type
andreturn type
do not supportNone
. -
The functions that are not supported in the custom function list are:
- Generator functions
- Async/Await functions
- Method definitions
- Class methods
- Default parameters
- Rest parameters
See Also see-also
- Create an AEM Adaptive Form
- Add an AEM Adaptive Form to AEM Sites page
- Apply themes to an AEM Adaptive Form
- Add components to an AEM Adaptive Form
- Use CAPTCHA in an AEM Adaptive Form
- Generate PDF version (DoR) of an AEM Adaptive Form
- Translate an AEM Adaptive Form
- Enable Adobe Analytics for an Adaptive Form to track form usage
- Connect Adaptive Form to Microsoft SharePoint
- Connect Adaptive Form to Microsoft Power Automate
- Connect Adaptive Form to Microsoft OneDrive
- Connect Adaptive Form to Microsoft Azure Blob Storage
- Connect Adaptive Form to Salesforce
- Use Adobe Sign in an AEM Adaptive Form
- Add a new locale for an Adaptive Form
- Send Adaptive Form data to a database
- Send Adaptive Form data to a REST endpoint
- Send Adaptive Form data to AEM Workflow
- Use Forms Portal to list AEM Adaptive Forms on an AEM website
- Add versionings, comments, and annotations to an Adaptive Form
- Compare Adaptive Forms