Form Runner JavaScript API
Getting a reference to a form
You can get a reference to an object representing a Form Runner form with the getForm()
function:
The formIdOrElem
parameter is described below.
Identifying a form by id or element
The formIdOrElem
parameter used in APIs can be:
missing or
undefined
: this searches for the first Orbeon Forms form on the pagea
string
: this is the namespaced id of the forman HTML element: this is the HTML form element, or a descendant or an HTML form element
If the form is not found, null
is returned. If found, an object is returned, which contains methods described below.
The FormRunnerForm
object
FormRunnerForm
objectFinding a Form Runner control by name
The findControlsByName()
function returns the HTML element(s) corresponding to the given Form Runner control name.
NOTE: The control must be visible or it will not be found. In particular, if the control is in a hidden wizard page, the control will not be found.
A Form Runner control name is the name entered by the form author in Form Builder. Examples:
first-name
street-address
If no control is found, an empty array is returned.
If there are multiple controls with the same name, the array will contain multiple elements. This can happen in the following cases:
when controls are repeated, for example in a repeated grid or section
when controls appear in the main form and section template and/or in different section templates
On the other hand, if the control exists but is not shown at a given time, for example if it is in a hidden wizard page, the array will be empty.
Example:
Setting a control's value
The setControlValue()
function allows you to set the value of a Form Runner control.
Here is how to set the value of a text field called my-field
to the value "Hello!":
For single selection controls, you pass the index of the value to select, either as a string or as a number:
or:
For multiple selection controls, you pass a space-separated string of indexes:
The following controls are supported:
Text field
Plain text area
Dropdown
Checkboxes
Radio buttons
Number
Currency
Date
Time
Activating a form control
The activateControl()
function allows you to activate a Form Runner control. This is useful to activating buttons that are present within the form (as opposed to process buttons, which are typically placed at the bottom of the form). However, you can also activate other controls, in particular Text Fields.
The following controls are supported:
Text field (as if the user presses the Return or Enter key)
Button
Telling whether the form data is safe
Orbeon Forms supports the notion that form data can be "safe" or not: specifically, it is safe if it's been saved to a database.
This function allows you to tell whether the data is safe or not.
Example:
See also the set-data-status
action.
Activating a process button
Process buttons are typically buttons that are not directly part of the form, but placed at the bottom of the form. They include functions such as "Save", "Send", "Next", etc.
You can activate a process button with this function by specifying the button name. Example:
Adding and removing process callback functions
Form Runner simple processes now support a callback()
action server action that allows client-side callback functions to be called.
You add a callback function on the client using:
You remove a callback function on the client using:
The name
and fn
parameters must be the same as those passed to addCallback()
.
Finding a Form Runner control by name
[SINCE Orbeon Forms 2017.2]
The findControlsByName()
function returns the HTML element(s) corresponding to the given Form Runner control name.
Prefer using first the getForm()
function, and then, on the object returned, the findControlsByName()
function.
Setting a control's value
You can set a control's value using the XForms JavaScript API.
For example, here is how to set the value of a text field called my-field
to the value "Hello!":
NOTE: For dropdowns created with Form Builder, the API does not provide direct support as of Orbeon Forms 2019.1. You can do it with the following JavaScript:
Where the value you pass corresponds to the position of the item starting at 0
.
Prefer using first the getForm()
function, and then, on the object returned, the setControlValue()
function.
Telling whether the form data is safe
[SINCE Orbeon Forms 2019.2]
Prefer using first the getForm()
function, and then, on the object returned, the isFormDataSafe()
function.
Focusing on a control
[SINCE Orbeon Forms 2017.2]
The wizard.focus()
function sets keyboard focus on a Form Runner control by name, including toggling wizard pages first if needed.
This function doesn't have any effect if the control is readonly or non-relevant.
Example:
The optional repeatIndexes
parameter allows reaching controls within repeats. For example, with one level of repeat:
accesses the second iteration of the comment
field.
Similarly, for nested repeats, you add as many elements in the array as there are nested repeats:
When repeatIndexes
is not specified, if the field is repeated, a single field is selected following the current repeat indexes.
NOTE: This only supports the wizard's free
validation mode. lax
and strict
are not yet supported.
This now also works with the strict
and lax
validation modes. In these modes, the wizard will check whether the switch to the requested wizard page is allowed.
If so, it will switch and focus on the control.
If not, it will ignore the
focus()
request.
Listening for Error Summary navigation
[SINCE Orbeon Forms 2022.1]
For analytics purposes, it can be useful to capture when the user is interacting with the Error Summary.
You can do so with the errorSummary.addNavigateToErrorListener()
function:
Example:
The parameter to the function is an event object defined as follows:
The listener can be removed with removeNavigateToErrorListener()
by passing the same function object as parameter.
See also
Last updated