While you’re free to use FormKit
inputs by themselves, you’ll usually want to group them into a form. To do this, simply wrap your inputs in a <FormKit type="form">
.
The form
type will actively collect all the values from child inputs, using the name
of each input as the property name in the resulting data object (just like groups). You can also read and write to form values using v-model just as you would on any input.
A <FormKit type="form">
tracks the form's validation state and prevents users from submitting the form if any inputs are invalid.
As a convenience, the form
outputs a submit button automatically, and provided themes also include a loading spinner. You can alter this button with the submit-label
and submit-attrs
props, or disable with :actions="false"
.
Excluding backend functionality, here is a fully featured form with inputs (form
, text
, email
, password
), help text, labels, validation with custom messages, and error and submission handling:
You can populate an entire form by providing a value
prop to the <FormKit type="form">
. The value
prop should be an object of input name to input value pairs. You may also use v-model
to populate a form if you require two-way data binding.
v-model
data in your submit handler could lead to unintended form mutations. Instead, use the unbound copy of your form’s data that is passed to your submission handler.
Forms are usually submitted through user actions like clicking a submit button or hitting the enter
key on a text node within the form. Upon submission, the form (in sequence):
@submit-raw
event.submitted
state to true on all inputs — displaying any remaining validation errors (regardless of the validation-visibility
).@submit
event.@submit
event returns a Promise
sets the form’s state to loading
until it resolves.The most common method of form submission in a modern SPA is an XHR request (think axios or fetch). FormKit is well suited to this task:
loading
becomes true at context.state.loading
and a spinner is displayed on the genesis
theme).To submit a form via page request, simply leave off the @submit
handler. Just like native HTML, you can also provide an action
and optionally a method
attribute.
Forms will not submit until all the inputs in the form are passing their validation rules.
In addition to not firing the submit event, a message is displayed above the submit button indicating the form is still incomplete. You can customize this message using the incomplete-message
prop or disable it by setting the prop to false
.
ui.incomplete
.
The validity of all inputs within a form is tracked automatically in the context object. This can be useful when creating various interfaces. For example, if you wanted a submit button to be disabled until all inputs are valid, you could use the state.valid
property to do so.
#default
slot, but there are other ways as well. The context object is available on each input’s core node on the node.context
property, and you can fetch an input’s node a number of ways.
To disable all the inputs in a given form, including the submit button, you can use the disabled
prop.
@submit
handler FormKit will automatically disable the form (and set the state to loading
) while the submit handler is pending.
With FormKit, adding front end validation to your form is easy — but what about errors produced by your backend framework, or ones you want to manually assign? There are two types of errors you can assign to a form:
Form errors (ones that apply to the entire form) can be set two ways.
errors
prop on a <FormKit type="form">
.$formkit.setErrors()
Vue plugin method.errors
propLike with any FormKit input, you can directly assign errors using the errors
prop. These errors are always visible (not subject to validation-visibility
).
$formkit.setErrors()
Alternatively, you can set errors directly on a form by giving the form an id
and then calling $formkit.setErrors('id', ['Form error here'])
. The setErrors
method must be passed the id
of the form, and then can handle 1 or 2 more arguments — the form errors, and the input errors.
setErrors
by importing it directly from @formkit/vue
.import { setErrors } from '@formkit/vue'
setErrors
method also works on the group
and list
input type. Just provide the input an id
to the input, and use it the exact same way.
Input errors (ones to be displayed with specific inputs in a form) can be applied three ways:
errors
prop on each individual input.input-errors
prop on the form (also works with groups and lists).$formkit.setErrors()
Vue plugin method (see example above).errors
propThe most basic way to display errors on a form is using the errors
prop that is available on each FormKit
input.
input-errors
propYou can also conveniently set error messages for all inputs in your form (or group or list) using the input-errors
prop. The prop accepts an object of errors, where the keys are input names (relative node addresses are supported) and the value is an error or array of errors to apply to that input.
When inputs are unmounted from a form — for example when using v-if
— the key and value is removed from the form’s data. However, in some circumstances it may be preferable to keep the key/value pair even after the input has been removed. This can be accomplished by using the preserve
prop.
Forms are technically considered input
types — so they share many of the universal props that standard inputs use.
Prop | Type | Default | Description |
---|---|---|---|
disabled | Boolean | false | Disables the form submit button and all the inputs in the form. |
incomplete-message | String/Boolean | {locale}.ui.incomplete | The message that is shown to near the submit button when a user attempts to submit a form, but not all inputs are valid. |
submit-attrs | Object | {} | Attributes or props that should be passed to the built-in submit button. |
submit-behavior | String | disabled | Async submit handlers automatically disable the form while pending, you can change this by setting this prop to 'live'. |
submit-label | String | Submit | The label to use on the built-in submit button. |
actions | Boolean | true | Whether or not to include the actions bar at the bottom of the form (ex. you want to remove the submit button and use your own, set this to false ). |
Show Universal Props | |||
config | Object | {} | Configuration options to provide to the input’s node and any descendent node of this input. |
delay | Number | 20 | Number of milliseconds to debounce an input’s value before the commit hook is dispatched. |
errors | Array | [] | Array of strings to show as error messages on this field. |
help | String | '' | Text for help text associated with the input. |
id | String | input_{n} | The unique id of the input. Providing an id also allows the input’s node to be globally accessed. |
ignore | Boolean | false | Prevents an input from being included in any parent (group, list, form etc). Useful when using inputs for UI instead of actual values. |
label | String | '' | Text for the label element associated with the input. |
name | String | input_{n} | The name of the input as identified in the data object. This should be unique within a group of fields. |
preserve | boolean | false | Preserves the value of the input on a parent group, list, or form when the input unmounts. |
sections-schema | Object | {} | An object of section keys and schema partial values, where each schema partial is applied to the respective section. |
type | String | text | The type of input to render from the library. |
validation | String, Array | [] | The validation rules to be applied to the input. |
validation-visibility | String | blur | Determines when to show an input's failing validation rules. Valid values are blur , dirty , and live . |
validation-label | String | {label prop} | Determines what label to use in validation error messages, by default it uses the label prop if available, otherwise it uses the name prop. |
validation-rules | Object | {} | Additional custom validation rules to make available to the validation prop. |
Section-key | Description |
---|---|
form | Responsible for rendering the form tag and listening to submit events. |
actions | Responsible for a container at the bottom of the form with form actions like the submit button. |
submit | Responsible for a submit button — by default a FormKit input type submit . |
Show Universal Props | |
outer | The outermost wrapping element. |
wrapper | A wrapper around the label and input. |
label | The label of the input. |
prefix | Has output by default, but allows content directly before an input element. |
inner | A wrapper around the actual input element. |
suffix | Has output by default, but allows content directly after an input element. |
input | The input element itself. |
help | The element containing help text. |
messages | A wrapper around all the messages. |
message | The element (or many elements) containing a message — most often validation and error messages. |