### Install @vuehookform/core dependencies Source: https://github.com/vuehookform/core/blob/main/docs/guide/quick-start.md Commands to install the core library and Zod validation package using various package managers. ```npm npm install @vuehookform/core zod ``` ```pnpm pnpm add @vuehookform/core zod ``` ```yarn yarn add @vuehookform/core zod ``` ```bun bun add @vuehookform/core zod ``` -------------------------------- ### Implement a complete login form with Vue Hook Form Source: https://github.com/vuehookform/core/blob/main/docs/guide/quick-start.md A full example demonstrating schema definition, form initialization, input registration, and submission handling in a Vue component. ```vue ``` -------------------------------- ### Configure form validation and defaults Source: https://github.com/vuehookform/core/blob/main/docs/guide/quick-start.md Examples of how to set default form values and configure validation trigger modes. ```typescript // Setting default values const { register, handleSubmit, formState } = useForm({ schema, defaultValues: { email: 'user@example.com', password: '', }, }) // Configuring validation modes useForm({ schema, mode: 'onSubmit', // 'onSubmit', 'onBlur', 'onChange', 'onTouched' }) ``` -------------------------------- ### Install Dependencies Source: https://github.com/vuehookform/core/blob/main/README.md Command to install the core library and Zod for schema validation. ```bash npm install @vuehookform/core zod ``` -------------------------------- ### Basic Form Setup with Schema Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Demonstrates the most basic setup for `useForm` which requires only a Zod schema for validation. This initializes the form with validation rules and provides essential methods like `register` and `handleSubmit`. ```typescript import { useForm } from '@vuehookform/core' import { z } from 'zod' const schema = z.object({ name: z.string().min(1, 'Name is required'), }) const { register, handleSubmit } = useForm({ schema }) ``` -------------------------------- ### Vue Example: Form Preview with All Fields Watched Source: https://github.com/vuehookform/core/blob/main/docs/api/use-watch.md Illustrates using useWatch to get all form data reactively, enabling features like a live preview. The example watches all fields and displays them in a separate preview area. ```vue ``` -------------------------------- ### Create Custom Input Component Source: https://github.com/vuehookform/core/blob/main/docs/api/use-controller.md A complete example of a reusable custom input component using useController to bind field state and validation errors. ```vue ``` -------------------------------- ### Implement Multi-Step Wizard Validation Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Demonstrates how to trigger partial form validation to control navigation between wizard steps. ```vue ``` -------------------------------- ### Form Setup with Default Values Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Shows how to pre-populate form fields using the `defaultValues` option in `useForm`. The provided default values must match the structure defined by the schema, and TypeScript will enforce this. This is useful for editing existing data or providing initial user settings. ```typescript const form = useForm({ schema, defaultValues: { email: 'user@example.com', age: 25, preferences: { newsletter: true, }, }, }) ``` -------------------------------- ### Vue Example: Watch Single Field Source: https://github.com/vuehookform/core/blob/main/docs/api/use-watch.md A complete Vue component example demonstrating how to use useWatch to display a single field's value reactively. It includes setup with useForm and rendering an input linked to the watched field. ```vue ``` -------------------------------- ### Form Setup with Async Default Values Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Demonstrates configuring `useForm` to load default values asynchronously, typically from an API. The `defaultValues` option accepts an async function that returns the initial data. Error handling for the async operation is managed via `onDefaultValuesError`. ```typescript const { formState } = useForm({ schema, defaultValues: async () => { const response = await fetch('/api/user') return response.json() }, onDefaultValuesError: (error) => { console.error('Failed to load:', error) }, }) ``` -------------------------------- ### useWatch Return Value Examples Source: https://github.com/vuehookform/core/blob/main/docs/api/use-watch.md Provides examples of the return types for useWatch when watching single fields, multiple fields, and all fields. The hook returns reactive refs containing the watched values. ```typescript // Single field const email: Ref = useWatch({ control, name: 'email' }) // Multiple fields const values: Ref<[string, string]> = useWatch({ control, name: ['email', 'password'], }) // All fields const all: Ref = useWatch({ control }) ``` -------------------------------- ### Implement Basic Form with Validation Source: https://github.com/vuehookform/core/blob/main/README.md A complete example showing how to initialize a form with Zod schema, register inputs, and handle submission in a Vue 3 component. ```vue ``` -------------------------------- ### Vue Example: Watching Multiple Fields for Combined Value Source: https://github.com/vuehookform/core/blob/main/docs/api/use-watch.md Demonstrates watching multiple specific fields and combining their values using Vue's computed property. This example creates a 'fullName' from 'firstName' and 'lastName'. ```vue ``` -------------------------------- ### Vue Example: Computing Derived Values Source: https://github.com/vuehookform/core/blob/main/docs/api/use-watch.md Shows how to use useWatch in conjunction with Vue's computed property to derive values based on other form fields. This example calculates a 'total' price from 'quantity' and 'unitPrice'. ```vue ``` -------------------------------- ### Implement Dynamic Field Arrays in Vue Source: https://github.com/vuehookform/core/blob/main/docs/guide/dynamic/field-arrays.md Demonstrates the basic setup of a dynamic form using VueHookForm's fields API. It includes schema definition with Zod, form initialization, and rendering repeatable input fields. ```vue ``` -------------------------------- ### Share Form Context via Provide/Inject Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Uses provideForm to share form state and methods across deeply nested component trees. ```vue ``` -------------------------------- ### Field Array Setup with Zod Schema in TypeScript Source: https://github.com/vuehookform/core/blob/main/docs/public/llms.txt Shows the setup for a field array using Vue Hook Form, including defining a Zod schema for validation and using 'fields' and 'register' to manage dynamic lists of form elements. ```typescript const schema = z.object({ items: z.array(z.object({ name: z.string().min(1), quantity: z.number().min(1) })).min(1) }) const { fields, register } = useForm({ schema, defaultValues: { items: [{ name: '', quantity: 1 }] } }) const itemFields = fields('items') ``` -------------------------------- ### Implement Dynamic Field Array with Scoped Methods Source: https://github.com/vuehookform/core/blob/main/docs/guide/dynamic/field-arrays.md A complete example of a dynamic form using @vuehookform/core, demonstrating scoped registration, error handling, and item manipulation. ```vue ``` -------------------------------- ### Implement User Registration Form with Validation Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Demonstrates a basic registration form using Zod for schema validation. It includes a custom refinement to ensure password and confirmPassword fields match. ```vue ``` -------------------------------- ### Form Setup with Disabled State Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Shows how to disable the entire form using a reactive `ref` passed to the `disabled` option in `useForm`. When disabled, all inputs are marked with the `disabled` attribute, and form submission is prevented, which is useful during loading states. ```typescript import { ref } from 'vue' const isLoading = ref(false) const form = useForm({ schema, disabled: isLoading, // Reactive ref }) ``` -------------------------------- ### Conventional Commits for Release Management Source: https://github.com/vuehookform/core/blob/main/CLAUDE.md Examples of Git commit messages following conventional commit standards for triggering automated releases with Release Please. Covers patch, minor, and major version bumps, as well as non-release commits. ```bash # Patch release (0.3.1 → 0.3.2) git commit -m "fix: correct validation error message formatting" # Minor release (0.3.2 → 0.4.0) git commit -m "feat: add onTouched validation mode" # Major release (0.4.0 → 1.0.0) git commit -m "feat!: change register() return type" # or git commit -m "feat: new API BREAKING CHANGE: register() now returns an object instead of array" # No release triggered git commit -m "docs: update README examples" git commit -m "chore: update CI workflow" ``` -------------------------------- ### Provide and Use Form Context in Vue Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Demonstrates how to provide a form instance to all descendant components using `provideForm` and access it in deeply nested components using `useFormContext`. This pattern avoids prop drilling in complex component trees. ```vue ``` ```vue ``` ```vue ``` -------------------------------- ### Vue Example: Conditional Rendering based on Field Value Source: https://github.com/vuehookform/core/blob/main/docs/api/use-watch.md Demonstrates using useWatch to control UI rendering based on a specific form field's value. The example shows conditional display of input fields based on the 'accountType' selection. ```vue ``` -------------------------------- ### Configure Dynamic Validation Modes Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Illustrates how to switch between different validation modes (onSubmit, onBlur, onChange, onTouched) dynamically. It uses a shallowRef to re-initialize the form instance when the mode changes. ```vue ``` -------------------------------- ### Implement Form Management with @vuehookform/core and Zod Source: https://github.com/vuehookform/core/blob/main/docs/guide/advanced/programmatic-control.md This example demonstrates how to initialize a form with Zod validation schemas, handle field registration, manage form state, and perform manual operations like resetting, triggering validation, and setting server-side errors. ```vue ``` -------------------------------- ### Create Dynamic Shopping Cart Form Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Shows how to manage a dynamic list of items using field arrays. Users can append or remove items, with each item validated against a quantity constraint. ```vue ``` -------------------------------- ### Field Array Rendering and Manipulation in Vue Source: https://github.com/vuehookform/core/blob/main/docs/public/llms.txt Provides a Vue template example for rendering and managing a field array. It shows how to iterate over fields, bind inputs using 'register', and handle adding/removing items. ```vue
``` -------------------------------- ### Form Configuration with Zod Schema Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Illustrates how to configure `useForm` with a Zod schema, defining field types and validation rules such as email format and minimum age. The schema dictates the structure and constraints of the form data. ```typescript const schema = z.object({ email: z.email(), age: z.number().min(18), preferences: z.object({ newsletter: z.boolean(), }), }) const form = useForm({ schema }) ``` -------------------------------- ### Form Configuration with Validation Mode Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Explains how to control when form validation occurs using the `mode` option in `useForm`. Available modes include 'onSubmit', 'onBlur', 'onChange', and 'onTouched', allowing developers to tailor the validation experience. ```typescript const form = useForm({ schema, mode: 'onBlur', // 'onSubmit' | 'onBlur' | 'onChange' | 'onTouched' }) ``` -------------------------------- ### Configure useController Options Source: https://github.com/vuehookform/core/blob/main/docs/api/use-controller.md Demonstrates how to initialize useController with specific options like name, control, and custom default values. ```typescript const controller = useController({ name: 'country', control, defaultValue: 'US', }) ``` -------------------------------- ### Dynamic Field Arrays with Vue Hook Form Core Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Manages dynamic arrays of form fields, supporting adding, removing, and validation. It emphasizes using `field.key` for Vue's `:key` binding and requires the `fields` function to be called within the setup context. ```vue ``` -------------------------------- ### Initialize useController Source: https://github.com/vuehookform/core/blob/main/docs/api/use-controller.md Basic implementation of the useController composable to manage a specific form field. It requires the field name and the control object from useForm. ```typescript import { useController } from '@vuehookform/core' const { field, fieldState } = useController({ name: 'email', control, }) ``` -------------------------------- ### Complete Form Example with Validation and API (Vue) Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/submission.md A comprehensive Vue.js example using Vue Hook Form for form management. It includes schema validation with Zod, handling form submission to an API endpoint, displaying server errors, and managing submission state. The `useForm` hook is central to this example. ```vue ``` -------------------------------- ### Initialize Form with Async Default Values Source: https://github.com/vuehookform/core/blob/main/docs/guide/advanced/async-patterns.md Demonstrates how to fetch initial form data from an API using an async function within the useForm configuration. It also shows how to use isLoading to manage UI states during the fetch. ```vue ``` -------------------------------- ### Configure form options Source: https://github.com/vuehookform/core/blob/main/docs/api/use-form.md Various configuration options for useForm including default values, validation modes, and performance settings like debouncing. ```typescript const form = useForm({ schema, defaultValues: { email: 'user@example.com', age: 25 }, mode: 'onChange', validationDebounce: 150, delayError: 300 }) ``` -------------------------------- ### useForm Return Values Overview Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Lists the comprehensive set of values returned by the `useForm` hook, including methods for input registration, submission handling, form state management, programmatic control, validation, error manipulation, and focus management. ```typescript const { // Register inputs register, unregister, // Handle form submission handleSubmit, // Reactive form state formState, // Field array management fields, // Programmatic control setValue, getValue, getValues, getFieldState, reset, resetField, // Validation trigger, // Manually trigger validation // Error management setError, setErrors, clearErrors, hasErrors, getErrors, // Watch field values watch, // Focus management setFocus, // For child components control, } = useForm({ schema }) ``` -------------------------------- ### Vue Example: Watching Field in Child Component Source: https://github.com/vuehookform/core/blob/main/docs/api/use-watch.md Shows how to watch a form field from a parent component within a child component using the 'control' prop. This example calculates password strength based on the 'password' field. ```vue ``` -------------------------------- ### Calling fields() Outside of Setup Source: https://github.com/vuehookform/core/blob/main/docs/public/llms.txt Discourages calling the `fields()` function directly within the template of a Vue component. It recommends fetching the field array in the `setup` function and then using the result in the template for better performance and maintainability. ```vue ``` -------------------------------- ### Vue Hook Form Error Structure Example Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/error-handling.md Illustrates the nested structure of validation errors in Vue Hook Form, which mirrors the schema definition. This example shows how to access errors for nested objects, arrays, and their properties. ```typescript const schema = z.object({ email: z.email(), profile: z.object({ name: z.string(), bio: z.string(), }), items: z.array( z.object({ title: z.string(), }), ), }) // Accessing errors formState.value.errors.email // string | undefined formState.value.errors.profile?.name // string | undefined formState.value.errors.items?.[0]?.title // string | undefined ``` -------------------------------- ### Initialize useForm with Zod schema Source: https://github.com/vuehookform/core/blob/main/docs/api/use-form.md Basic usage of the useForm composable to manage form state using a Zod validation schema. ```typescript import { useForm } from '@vuehookform/core' const form = useForm({ schema: z.object({ email: z.email(), password: z.string().min(8), }), }) ``` -------------------------------- ### Track Async Loading States Source: https://github.com/vuehookform/core/blob/main/docs/guide/advanced/async-patterns.md Explains the use of isLoading and isReady properties to provide feedback to users while the form is initializing. ```vue ``` -------------------------------- ### Form Setup with Field Unregistration Control Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/form-setup.md Configures `useForm` to control whether field data is removed when a field unmounts using `shouldUnregister`. Setting it to `true` removes field data, useful for dynamic forms, while `false` (default) retains data for persistence, suitable for multi-step forms. ```typescript const form = useForm({ schema, shouldUnregister: true, // Remove data when fields unmount (default: false) }) ``` -------------------------------- ### Handling Dynamic Paths with Loose Overloads Source: https://github.com/vuehookform/core/blob/main/docs/api/use-form.md Demonstrates how loose overloads allow dynamic path construction, such as array indexing, which would otherwise fail TypeScript's strict template literal type checking. ```typescript const index = 0; // Without loose overloads, this would require a cast register(`items.${index}.name`); // TypeScript can't verify this at compile time // With loose overloads, it works without casts register(`items.${index}.name`); // ✅ Accepted by loose overload ``` -------------------------------- ### Handle Validation Modes Source: https://github.com/vuehookform/core/blob/main/docs/api/use-controller.md Shows how useController respects form validation modes and reValidateMode settings after submission. ```typescript const { control } = useForm({ schema, mode: 'onSubmit', reValidateMode: 'onChange', }) const { field } = useController({ name: 'email', control }) ``` -------------------------------- ### Avoid Non-Reactive Error Display Patterns Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Highlights a common anti-pattern where getFieldState is used incorrectly, resulting in non-reactive error messages that fail to update. ```vue ``` -------------------------------- ### Initialize Form State with useForm Source: https://github.com/vuehookform/core/blob/main/docs/guide/index.md Demonstrates the core composable used to manage form state, including registration, submission handling, and state tracking based on a provided schema. ```typescript const { register, handleSubmit, formState, fields } = useForm({ schema }) ``` -------------------------------- ### Implement Reusable Components with LooseControl Source: https://github.com/vuehookform/core/blob/main/docs/api/use-controller.md Demonstrates the use of LooseControl to create generic, reusable form components that work across different form schemas without explicit type casting. ```vue ``` -------------------------------- ### Handle Async Initialization Errors Source: https://github.com/vuehookform/core/blob/main/docs/guide/advanced/async-patterns.md Shows how to catch errors during async default value loading using onDefaultValuesError and how to display error states in the template. ```typescript const { formState } = useForm({ schema, defaultValues: async () => { const response = await fetch('/api/user/profile') if (!response.ok) { throw new Error('Failed to load profile') } return response.json() }, onDefaultValuesError: (error) => { console.error('Failed to load form data:', error) toast.error('Could not load your profile data') }, }) ``` ```vue ``` -------------------------------- ### Define Survey Schema with Discriminated Unions Source: https://github.com/vuehookform/core/blob/main/docs/examples/index.md Utilizes Zod's discriminated unions to handle heterogeneous form structures, such as different question types in a survey. ```vue ``` -------------------------------- ### Field Arrays: Scoped Methods vs Loose Overloads Source: https://github.com/vuehookform/core/blob/main/docs/api/use-form.md Compares the recommended approach of using scoped methods for full type safety against using loose overloads for dynamic path construction in field arrays. ```typescript const items = fields('items'); // Option 1: Scoped methods (recommended) - full type safety items.value.forEach((field) => { field.register('name'); // ✅ Full type safety }); // Option 2: Loose overloads - no cast needed items.value.forEach((field) => { register(`items.${field.index}.name`); // ✅ Works without cast }); ``` -------------------------------- ### Display Validation Errors in Vue Hook Form Source: https://github.com/vuehookform/core/blob/main/docs/guide/essentials/submission.md Provides an example of how to display client-side validation errors associated with specific form fields. It checks `formState.value.errors` for each field. ```vue ``` -------------------------------- ### Compare Manual vs Scoped Field Registration Source: https://github.com/vuehookform/core/blob/main/docs/guide/dynamic/field-arrays.md Shows the difference between manually constructing paths for form fields versus using scoped methods provided by field array items for cleaner, type-safe code. ```vue ``` -------------------------------- ### Logging Form State with watchEffect Source: https://github.com/vuehookform/core/blob/main/docs/guide/troubleshooting.md Provides a TypeScript example using `watchEffect` to log changes in form state, including errors, dirty status, and validity, which is useful for debugging. ```typescript import { watchEffect } from 'vue' watchEffect(() => { console.log('Form state:', { errors: formState.value.errors, isDirty: formState.value.isDirty, isValid: formState.value.isValid, }) }) ``` -------------------------------- ### Validation Mode Configuration Source: https://github.com/vuehookform/core/blob/main/docs/guide/dynamic/field-arrays.md Examples showing how different validation modes affect field array operations. Operations trigger validation based on the form's configured mode. ```typescript // Mode: onSubmit - operations don't trigger validation const { fields } = useForm({ schema, mode: 'onSubmit' }); // Mode: onChange - operations trigger validation const { fields } = useForm({ schema, mode: 'onChange' }); ``` -------------------------------- ### Vue Hook Form Core Commands Source: https://github.com/vuehookform/core/blob/main/CLAUDE.md Common npm commands for building, type-checking, linting, formatting, serving documentation, and running tests for the Vue Hook Form library. ```bash npm run build npm run type-check npm run lint npm run format npm run docs npm test npm run test:run ```