--- description: Collect and validate form data. links: - label: GitHub icon: i-simple-icons-github to: https://github.com/nuxt/ui/blob/dev/src/runtime/components/forms/Form.vue --- ## Usage Use the Form component to validate form data using schema libraries such as [Yup](https://github.com/jquense/yup), [Zod](https://github.com/colinhacks/zod), [Joi](https://github.com/hapijs/joi), [Valibot](https://valibot.dev/) or your own validation logic. It works seamlessly with the FormGroup component to automatically display error messages around form elements. The Form component requires the `validate` and `state` props for form validation. - `state` - a reactive object that holds the current state of the form. - `validate` - a function that takes the form's state as input and returns an array of `FormError` objects with the following fields: - `message` - the error message to display. - `path` - the path to the form element matching the `name`. ::component-example #default :form-example-basic{class="space-y-4 w-60"} #code ```vue ``` :: ## Schema You can provide a schema from [Yup](#yup), [Zod](#zod) or [Joi](#joi), [Valibot](#valibot) through the `schema` prop to validate the state. It's important to note that **none of these libraries are included** by default, so make sure to **install the one you need**. ### Yup ::component-example #default :form-example-yup{class="space-y-4 w-60"} #code ```vue ``` :: ### Zod ::component-example #default :form-example-zod{class="space-y-4 w-60"} #code ```vue ``` :: ### Joi ::component-example #default :form-example-joi{class="space-y-4 w-60"} #code ```vue ``` :: ### Valibot ::component-example #default :form-example-valibot{class="space-y-4 w-60"} #code ```vue ``` :: ## Other libraries For other validation libraries, you can define your own component with custom validation logic. Here is an example with [Vuelidate](https://github.com/vuelidate/vuelidate): ```vue ``` ## Backend validation You can manually set errors after form submission if required. To do this, simply use the `form.setErrors` function to set the errors as needed. ```vue ``` ## Input events The Form component automatically triggers validation upon `submit`, `input`, `blur` or `change` events. This ensures that any errors are displayed as soon as the user interacts with the form elements. You can control when validation happens this using the `validate-on` prop. ::component-example #default :form-example-elements{class="space-y-4 w-60"} :: ## Props :component-props ## API ::field-group ::field{name="validate (path?: string, opts: { silent?: boolean })" type="Promise"} Triggers form validation. Will raise any errors unless `opts.silent` is set to true. :: ::field{name="clear (path?: string)" type="void"} Clears form errors associated with a specific path. If no path is provided, clears all form errors. :: ::field{name="getErrors (path?: string)" type="FormError[]"} Retrieves form errors associated with a specific path. If no path is provided, returns all form errors. :: ::field{name="setErrors (errors: FormError[], path?: string)" type="void"} Sets form errors for a given path. If no path is provided, overrides all errors. :: ::field{name="errors" type="Ref"} A reference to the array containing validation errors. Use this to access or manipulate the error information. :: ::