6.5 KiB
description, category, links
| description | category | links | |||||||
|---|---|---|---|---|---|---|---|---|---|
| A form component with built-in validation and submission handling. | form |
|
Usage
Use the Form component to validate form data using validation libraries such as Valibot, Zod, Yup, Joi, Superstruct or your own validation logic.
It works with the FormField component to display error messages around form elements automatically.
Schema Validation
It requires two props:
state- a reactive object holding the form's state.schema- any Standard Schema or a schema from Yup, Joi or Superstruct.
::warning No validation library is included by default, ensure you install the one you need. ::
::tabs{class="gap-0"} ::component-example
name: 'form-example-valibot' props: class: 'w-60'
::
::component-example
name: 'form-example-zod' props: class: 'w-60'
::
::component-example
name: 'form-example-yup' props: class: 'w-60'
::
::component-example
name: 'form-example-joi' props: class: 'w-60'
::
::component-example
name: 'form-example-superstruct' props: class: 'w-60'
:: ::
Errors are reported directly to the FormField component based on the name or error-pattern prop. This means the validation rules defined for the email attribute in your schema will be applied to <FormField name="email">{lang="vue"}.
Nested validation rules are handled using dot notation. For example, a rule like { user: z.object({ email: z.string() }) }{lang="ts"} will be applied to <FormField name="user.email">{lang="vue"}.
Custom Validation
Use the validate prop to apply your own validation logic.
The validation function must return a list of errors with the following attributes:
message- the error message to display.name- thenameof theFormFieldto send the error to.
::tip
It can be used alongside the schema prop to handle complex use cases.
::
::component-example
name: 'form-example-basic' props: class: 'w-60'
::
Input Events
The Form component automatically triggers validation when an input emits an input, change, or blur event.
- Validation on
inputoccurs as you type. - Validation on
changeoccurs when you commit to a value. - Validation on
blurhappens when an input loses focus.
You can control when validation happens this using the validate-on prop.
::component-example
source: false name: 'form-example-elements' options:
- name: 'validate-on'
label: 'validate-on'
items:
- 'input'
- 'change'
- 'blur' default:
- 'input'
- 'change'
- 'blur' multiple: true
::
::tip
You can use the useFormField composable to implement this inside your own components.
::
Error Event
You can listen to the @error event to handle errors. This event is triggered when the form is submitted and contains an array of FormError objects with the following fields:
id- the input'sid.name- thenameof theFormFieldmessage- the error message to display.
Here's an example that focuses the first input element with an error after the form is submitted:
::component-example
name: 'form-example-on-error' collapse: true props: class: 'w-60'
::
Nesting Forms
Nesting form components allows you to manage complex data structures, such as lists or conditional fields, more efficiently.
For example, it can be used to dynamically add fields based on user's input: ::component-example
collapse: true name: 'form-example-nested'
::
Or to validate list inputs: ::component-example
collapse: true name: 'form-example-nested-list'
::
API
Props
:component-props
Slots
:component-slots
Emits
:component-emits
Expose
You can access the typed component instance using useTemplateRef.
<script setup lang="ts">
const form = useTemplateRef('form')
</script>
<template>
<UForm ref="form" />
</template>
This will give you access to the following:
| Name | Type |
|---|---|
submit(){lang="ts-type"} |
Promise<void>{lang="ts-type"} Triggers form submission. |
validate(opts: { name?: keyof T | (keyof T)[], silent?: boolean, nested?: boolean, transform?: boolean }){lang="ts-type"} |
Promise<T>{lang="ts-type"} Triggers form validation. Will raise any errors unless |
clear(path?: keyof T){lang="ts-type"} |
void Clears form errors associated with a specific path. If no path is provided, clears all form errors. |
getErrors(path?: keyof T){lang="ts-type"} |
FormError[]{lang="ts-type"} Retrieves form errors associated with a specific path. If no path is provided, returns all form errors. |
setErrors(errors: FormError[], name?: keyof T){lang="ts-type"} |
void Sets form errors for a given path. If no path is provided, overrides all errors. |
errors{lang="ts-type"} |
Ref<FormError[]>{lang="ts-type"} A reference to the array containing validation errors. Use this to access or manipulate the error information. |
disabled{lang="ts-type"} |
Ref<boolean>{lang="ts-type"} |
dirty{lang="ts-type"} |
Ref<boolean>{lang="ts-type"} true if at least one form field has been updated by the user. |
dirtyFields{lang="ts-type"} |
DeepReadonly<Set<keyof T>>{lang="ts-type"} Tracks fields that have been modified by the user. |
touchedFields{lang="ts-type"} |
DeepReadonly<Set<keyof T>>{lang="ts-type"} Tracks fields that the user interacted with. |
blurredFields{lang="ts-type"} |
DeepReadonly<Set<keyof T>>{lang="ts-type"} Tracks fields blurred by the user. |
Theme
:component-theme