JavaScript Utilities
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.
or they can be used as part of the Form.io renderer by including the following script in your application.
and then you can find the Utilities methods as part of the Utils
property on the Formio
object.
Utility Functions
The following are common functions that can be used within the Form.io Utilities library.
eachComponent(components, fn, [includeAll])
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)
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)
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)
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(component, query)
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)
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)
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)
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. |
parseFloat(value)
parseFloat(value)
Extension of standard parseFloat function, that also clears input string. Useful for Currency component.
Parameter | Description |
value | The value you would like to parse. |
formatAsCurrency(value)
formatAsCurrency(value)
Formats provided value in way how Currency component uses it.
Parameter | Description |
value | The value you would like to format. |
escapeRegExCharacters(value)
escapeRegExCharacters(value)
Escapes RegEx characters in provided String value.
Parameter | Description |
value | The value you would like to escape the regex characters from. |
boolValue(value)
boolValue(value)
Determines the boolean value of a setting.
isMongoId(text)
isMongoId(text)
Check to see if an ID is a mongoID.
checkCondition(component, row, data, form, instance)
checkCondition(component, row, data, form, instance)
Checks the conditions for a provided component and data.
Parameter | Description |
component | The component JSON |
row | Contextual row data |
data | Full submission data |
form | The form JSON schema |
instance | The component instance this method is being executed with. |
unescapeHTML(str)
unescapeHTML(str)
Unescape HTML characters like <, >, & and etc.
convertStringToHTMLElement(str, selector)
convertStringToHTMLElement(str, selector)
Make HTML element from string
guid()
guid()
Generate a GUID
isValidDate(date)
isValidDate(date)
Checks a date to see if it is valid.
formatDate(value, format, timezone, flatPickrInputFormat)
formatDate(value, format, timezone, flatPickrInputFormat)
Format a date provided a value, format, and timezone object.
getBrowserInfo()
getBrowserInfo()
Get the browser name and version
isInputComponent(componentJson)
isInputComponent(componentJson)
Checks to see if a component JSON is an input component.
Last updated