title |
---|
Patches |
Our patched version of Formio.createForm()
adds support for the following
options:
This sets the initial page of the form by its index (position) in the list of visible pages starting from 1 (the first page, 2 is the second, and so on). Also note:
- The
data
option is applied beforepage
, so if the desired page is conditionally hidden you will need to also provide data that will satisfy the conditions. - The
focus
option is applied afterpage
, and will override the page selection if the focused component is on a different page.
This attempts to focus a specific component by its unique "key" once it's loaded. Use this when testing validations on a specific field, or to scroll to a specific part of the form. Also note:
- The
data
option is applied beforefocus
, so if components (or their pages) are conditionally hidden you may need to pass data that will satisfy the conditions. - The
page
option is applied beforefocus
, but you shouldn't need to use both because the form will set the initial page automatically in order to focus the right component.
If provided, the data
option will be passed along as the form's initial
submission. See also: the prefill
option.
If googleTranslate
is false
, the translate="no"
attribute and
notranslate
class are added to the form element wrapper to prevent Google
Translate from touching it. This is preferable (but not required!) when
translations are provided via the i18n
option, since Google Translate will
attempt to translate any element that doesn't have the notranslate
class,
and may replace a human translation with a machine translation.
If the i18n
option is a string, it's treated as a JSON URL from which to
load localizations (translations of form content and field info). See the
localization docs for more info.
If the hooks
option is an object, any value that isn't a function is
converted to a declarative action. See formiojs's
hooks documentation
for the list of available hooks.
Like hooks
, the on
object can be used to specify declarative
actions for any of formiojs's
known form events.
The prefill
option allows you to pre-fill form inputs with submission
data:
-
The value
querystring
will cause pre-fill values to be parsed fromwindow.location.search
. E.g.?foo=bar
will initialze the form submission as{foo: 'bar'}
. -
The value
hash
will cause pre-fill values to be parsed fromwindow.location.hash
(afer the leading#
), so#foo=bar
will initialize the form submission as{foo: 'bar'}
. -
Otherwise, if
prefill
is an instance of URLSearchParams, the form submission will be initialized using its entries.
Setting the formioSFDSOptOut
option to true
disables all of the following
customizations for your form:
-
Scoped style modifications. Note: template modifications can't be opted out because they're provided at the theme level, so you'll need to style the selectors generated by this theme's templates, not the built-in ones!
-
Select components will not be rendered as plain old
<select>
elements by default, and theautocomplete
tag will be ignored. -
Custom event handlers will not be registered.
-
The
prefill
option will be ignored.
Unless otherwise noted, all of the upgrades below can be opted out of with the
formioSFDSOptOut
option.
-
Detects the form rendering language (locale) by looking for the closest element with a
lang
attribute. -
Select components are made to always use the
html5
"widget" (an HTML<select>
element), unless theautocomplete
tag also exists on the component (ifcomponent.tags.include('autocomplete')
). -
Form elements are wrapped in
<div class="formio-sfds">
, which allows the element itself to receive styles defined in the scoped CSS. -
All components get
validateOn: 'blur'
, which defers validation of fields until the input is blurred. The default ischange
, which triggers validation whenever an input changes, and can trigger disruptive validation errors while the user is typing.
The hooks
and on
options allow you to customize form behaviors using a
limited vocabulary of "declarative" actions. Each key of these objects is
the name of a hook or event, and its value is an object with a single key
that corresponds to one of the following actions:
-
redirect
takes either a URL string or an object with aurl
key and redirects (by settingwindow.location
) to the URL. Submission data values may be interpolated in the redirect URL as{key}
, wherekey
is the API key of the form input. For example:{ "on": { "submit": { "redirect": "/confirm?username={username}" } } }
-
validateWithService
passes the submission data to an HTTP web service for validation. Theurl
is the URL of the web service, and may contain form value interpolations (e.g.{username}
expands toform.submission.data.username
),method
tells it the HTTP verb (default:POST
), andmessages
is an optional object containing custom messages for different types of errors, such as{empty: "error message if the response was empty"}
. For instance, you might wish to validate a username field via an external service that would respond with an error if the provided username is already taken:{ "hooks": { "beforeSubmit": { "validateWithService": { "url": "https://some-validation-service.example.com/username/{username}" } } } }
If the web service fails, or if it's successful and the response JSON has an
errors
orerror
key, those are reported as errors and will abortbeforeSubmit
hooks. -
values
validates submissions by comparing the value of the submission data with each key in thevalues
object. For instance, to ensure that thefoo
form input has a value of"bar"
before submission:{ "hooks": { "beforeSubmit": { "values": { "foo": "bar" } } } }
SFDS icons are rendered using selector observer
to replace the contents of elements with a data-icon
attribute with the
corresponding SVG icon at runtime, i.e. turning this:
<span data-icon="next" aria-label="Next"></span>
into this:
<span data-icon="next" aria-label="Next">
<svg ...></svg>
</span>
FontAwesome icons (any element with an fa-*
class name) rendered by Formio
have the data-icon
attribute icon added, which in turn triggers the SVG icon
insertion described above, turning this:
<i class="fa-info some-class"></i>
into this:
<i data-icon="info" class="some-class">
<svg ...></svg>
</i>
See icons in Figma and the source for a full list of
valid data-icon
attribute values.