This section documents the utilities functions available within the JavaScript SDK
Introduction
The Form.io Utilities class is a part of the JavaScript SDK, but provides a number of methods that makes working with Form.io forms within your applications easier.
Installation
To install the Utilities, you can either import them directly into your application as follows.
import Utils from 'formiojs/utils';
or they can be used as part of the Form.io renderer by including the following script in your application.
Navigate to the following link for more information about the Form.io CDNs
and then you can find the Utilities methods as part of the Utils property on the Formio object.
Formio.Utils.eachComponent(....)
Utility Functions
The following are common functions that can be used within the Form.io Utilities library.
eachComponent(components, fn, [includeAll])
Calls fn(component) for each component in components, accounting for nested layout components. (Does not call for layout components themselves, unless includeAll is true).
Parameter
Description
components
An array of JSON form components to iterate over.
fn
The function to call for each input component.
includeAll
Boolean to indicate if we should include all components including layout components.
The current data path of the element. Example: data.user.firstName
getComponent(components, key)
Returns the component with the given key or undefined if not found.
Parameter
Description
components
An array of JSON form components to iterate over.
key
The key of the component you would like to retrieve.
findComponents(components, query)
Returns an array of components that match the find query criteria. This query is very similar to MongoDB where if you wish to find a nested query, you would provide the key as the path of the property using dot notation.
Parameter
Description
components
An array of JSON form components to iterate over.
query
A JSON object to use as a query to find a certain component within a form.
Here is an example.
searchComponents(components, query)
Finds a component provided a query of properties of that component.
Parameter
Description
components
An array of JSON form components to iterate over.
query
The query to pass to matchComponent.
matchComponent(component, query)
Matches if a component matches the query.
Parameter
Description
component
A component JSON you would like to match against a query.
query
A JSON query to use as the match query.
flattenComponents(components, includeAll)
Returns a key-value object where the keys are the keys for each component in components and each key points to the corresponding component. This includes nested components as well. Pass true for includeAll if you want to include layout components.
Parameter
Description
components
An array of JSON form components to iterate over.
includeAll
Boolean to indicate if you wish to include all components including layout components.
isLayoutComponent(component)
Determine if a component is a layout component.
Parameter
Description
component
The component JSON to determine if this is a layout component
getValue(submission, key)
Get the value for a components API key, from the given submission. Recursively searches the submission for the key.
Parameter
Description
submission
The submission JSON object
key
The component key you would like to fetch the value from within the submission.
var utils = require('formiojs/utils');
utils.eachComponent(form.components, function(component) {
// Do something...
})
var utils = require('formiojs/utils');
var component = utils.getComponent(form.components, 'myKey');
// Find all textfields with a specific custom property.
var utils = require('formiojs/utils');
var comps = utils.findComponents(form.components, {
'type': 'textfield',
'properties.objectId': '2345'
});
// Should return all textfield components with 'properties.objectId' = '2345'.
console.log(comps);