# Formisch The modular and type-safe form library for any framework. ## Hooks ### useForm Creates a reactive form store from a form configuration. The form store manages form state and provides reactive properties. ```ts const form = useForm(config); ``` #### Generics - `TSchema` #### Parameters - `config` ##### Explanation `useForm` creates a reactive form store that manages form state using the provided Valibot schema. Validation runs when a field is blurred or the form is submitted by default, but this can be customized with the `validate` and `revalidate` options. #### Returns - `form` #### Related The following APIs can be combined with `useForm`. ##### Hooks ##### Components ##### Methods ### useField Creates a reactive field store of a specific field within a form store. ```ts const field = useField(form, config); ``` #### Generics - `TSchema` - `TFieldPath` #### Parameters - `form` - `config` ##### Explanation With `useField` you create a reactive field store for a specific field path within your form. The field store provides reactive access to the field's input value, validation errors, touched state, and more. It also includes a `props` object that you can spread onto an HTML input or textarea element to automatically handle user interactions like focus, blur, and input events. #### Returns - `field` #### Related The following APIs can be combined with `useField`. ##### Hooks ##### Components ##### Methods ### useFieldArray Creates a reactive field array store of a specific field array within a form store. ```ts const fieldArray = useFieldArray(form, config); ``` #### Generics - `TSchema` - `TFieldArrayPath` #### Parameters - `form` - `config` ##### Explanation With `useFieldArray` you create a reactive field array store for a specific array field within your form. The field array store provides reactive access to the array items (by their IDs), validation errors, touched state, and more. This is particularly useful when working with dynamic arrays of fields that users can add or remove. #### Returns - `fieldArray` #### Related The following APIs can be combined with `useFieldArray`. ##### Hooks ##### Components ##### Methods ## Components ### Form Form component that manages form submission and applies internal state. Wraps form element and passes submission events to the provided handler. ```tsx
children
``` #### Generics - `TSchema` #### Properties - `of` - `children` - `onSubmit` ##### Explanation The `Form` component wraps a native HTML `
` element and provides form submission handling. It automatically prevents the default form submission behavior and calls the `onSubmit` handler when the form is submitted and validation succeeds. The component uses the `of` prop to reference the form store and renders the `children` elements within the form. It also sets `novalidate` on the form element to disable browser validation, as Formisch handles validation internally. #### Related ##### Hooks ##### Components ##### Methods ### Field Headless form field component that provides reactive properties and state. The field component takes a form store, path to field, and a render function that receives a field store to display field state and handle user interactions. ```tsx children ``` #### Generics - `TSchema` - `TFieldPath` #### Properties - `of` - `path` - `children` ##### Explanation The `Field` component is a headless component that doesn't render any UI itself. Instead, it provides a render prop function via the `children` prop that receives a `FieldStore` containing all the reactive state and props needed to render a form field. The component automatically handles field registration, validation, and state management through the `of` prop that references the form store. #### Related ##### Hooks ##### Components ### FieldArray Headless field array component that provides reactive properties and state for managing array fields. The field array component takes a form store, path to array field, and a render function that receives a field array store to manage array items and handle array operations. ```tsx children ``` #### Generics - `TSchema` - `TFieldArrayPath` #### Properties - `of` - `path` - `children` ##### Explanation The `FieldArray` component is a headless component that doesn't render any UI itself. Instead, it provides a render prop function via the `children` prop that receives a `FieldArrayStore` containing all the reactive state and methods needed to manage an array of form fields. The component automatically handles array field registration, validation, and state management through the `of` prop that references the form store. #### Related ##### Hooks ##### Components ##### Methods ## Methods ### focus Focuses the first input element of a field. This is useful for programmatically setting focus to a specific field, such as after validation errors or user interactions. ```ts focus(form, config); ``` #### Generics - `TSchema` - `TFieldPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store containing the field to focus. The `config` parameter specifies which field to focus using the `path` property, and can optionally include additional focus options. #### Related ##### Primitives ##### Methods ### getAllErrors Retrieves all error messages from all fields in the form by walking through the entire field store tree. This is useful for displaying a summary of all validation errors across the form. ```ts const errors = getAllErrors(form); ``` #### Parameters - `form` ##### Explanation The `form` parameter is the form store to retrieve errors from. The function walks through the entire field store tree to collect all validation error messages from every field. #### Returns - `result` #### Related ##### Primitives ##### Methods ### getErrors Retrieves error messages from the form. When called without a config, returns form-level errors. When called with a path, returns errors for that specific field. ```ts const errors = getErrors(form); const errors = getErrors(form, config); ``` #### Generics - `TSchema` - `TFieldPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store to retrieve errors from. The optional `config` parameter specifies which errors to retrieve - either form-level errors (when omitted) or field-specific errors (when a `path` is provided). #### Returns - `result` #### Related ##### Primitives ##### Methods ### getInput Retrieves the current input value of a specific field or the entire form. Returns a partial object as not all fields may have been set. ```ts const input = getInput(form); const input = getInput(form, config); ``` #### Generics - `TSchema` - `TFieldPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store to retrieve input from. The optional `config` parameter specifies which field to get input from - if omitted, returns input from the entire form. #### Returns - `result` #### Related ##### Primitives ##### Methods ### handleSubmit Creates a submit event handler for the form that prevents default browser submission, validates the form input, and calls the provided handler if validation succeeds. This is designed to be used with the form's onsubmit event. ```ts const result = handleSubmit(form, handler); ``` #### Generics - `TSchema` #### Parameters - `form` - `handler` ##### Explanation The `form` parameter is the form store to handle submission for. The `handler` parameter is the submit handler function that gets called with the validated form output if validation succeeds. #### Returns - `result` #### Related ##### Primitives ##### Methods ### insert Inserts a new item into a field array at the specified index. All items at or after the insertion point are shifted up by one index. ```ts insert(form, config); ``` #### Generics - `TSchema` - `TFieldArrayPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store containing the field array. The `config` parameter specifies the path to the field array, the index where to insert the new item, and optional initial input values for the new item. #### Related ##### Primitives ##### Methods ### move Moves an item within a field array from one index to another. All items between the old and new positions are shifted accordingly. ```ts move(form, config); ``` #### Generics - `TSchema` - `TFieldArrayPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store containing the field array. The `config` parameter specifies the path to the field array and the indices to move the item from and to. #### Related ##### Primitives ##### Methods ### remove Removes an item from a field array at the specified index. All items after the removed index are shifted down by one. ```ts remove(form, config); ``` #### Generics - `TSchema` - `TFieldArrayPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store containing the field array. The `config` parameter specifies the path to the field array and the index of the item to remove. #### Related ##### Primitives ##### Methods ### replace Replaces an item in a field array at the specified index with a new item. ```ts replace(form, config); ``` #### Generics - `TSchema` - `TFieldArrayPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store containing the field array. The `config` parameter specifies the path to the field array, the index of the item to replace, and the new item values. #### Related ##### Primitives ##### Methods ### reset Resets the form or a specific field to its initial state. Optionally keeps certain states like input values, touched state, or errors. ```ts reset(form); reset(form, config); ``` #### Generics - `TSchema` - `TFieldPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store to reset. The optional `config` parameter specifies which field to reset (or the entire form if omitted) and which states to preserve during the reset. #### Related ##### Primitives ##### Methods ### setErrors Sets or clears error messages on the form or a specific field. This is useful for setting custom validation errors that don't come from schema validation. ```ts setErrors(form, config); ``` #### Generics - `TSchema` - `TFieldPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store to set errors on. The `config` parameter specifies the path and error messages to set, or can be set to null to clear existing errors. #### Related ##### Primitives ##### Methods ### setInput Sets input values for the form or a specific field programmatically. ```ts setInput(form, config); setInput(form, config); ``` #### Generics - `TSchema` - `TFieldPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store to set input on. The `config` parameter specifies the path to the field and the new input values to set. #### Related ##### Primitives ##### Methods ### submit Programmatically requests form submission by calling the native `requestSubmit()` method on the underlying form element. ```ts submit(form); ``` #### Parameters - `form` ##### Explanation The `form` parameter is the form store to submit. The function triggers form submission by calling the native `requestSubmit()` method on the underlying form element. #### Related ##### Primitives ##### Methods ### swap Swaps two items in a field array at the specified indices. ```ts swap(form, config); ``` #### Generics - `TSchema` - `TFieldArrayPath` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store containing the field array. The `config` parameter specifies the path to the field array and the two indices of the items to swap. #### Related ##### Primitives ##### Methods ### validate Validates the entire form input against its schema. Returns a safe parse result indicating success or failure with detailed issues. Optionally focuses the first field with validation errors. ```ts const result = validate(form, config?); ``` #### Generics - `TSchema` #### Parameters - `form` - `config` ##### Explanation The `form` parameter is the form store to validate. The optional `config` parameter allows specifying whether to automatically focus the first field with validation errors after validation. #### Returns - `result` #### Related ##### Primitives ##### Methods ## Types ### DeepPartial A utility type that recursively makes all properties and nested properties optional. > This type is too complex to display. Please refer to the [source code](https://github.com/fabian-hiller/formisch/blob/main/packages/core/src/types/utils.ts). #### Related ##### Primitives ### FieldArrayStore Reactive field array store interface with properties for managing array fields. #### Generics - `TSchema` - `TFieldArrayPath` #### Definition - `FieldArrayStore` - `path` - `items` - `errors` - `isTouched` - `isDirty` - `isValid` ### FieldElement A union type representing form field elements that can be registered with a field store. #### Definition - `FieldElement` #### Related ##### Methods ### FieldElementProps Props to spread onto a field element for integration with Preact form handling. #### Definition - `FieldElementProps` - `name` - `autofocus` - `ref` - `onFocus` - `onInput` - `onChange` - `onBlur` ### FieldStore Reactive field store interface with properties and element props for field management. #### Generics - `TSchema` - `TFieldPath` #### Definition - `FieldStore` - `path` - `input` - `errors` - `isTouched` - `isDirty` - `isValid` - `props` ### FocusFieldConfig Configuration interface for focusing a field. Used by the `focus` method to specify which field to focus and any additional focus options. #### Generics - `TSchema` - `TFieldPath` #### Definition - `FocusFieldConfig` - `path` #### Related ##### Methods ### FormConfig Form config interface that defines the configuration options for creating a form store with `useForm`. #### Generics - `TSchema` #### Definition - `FormConfig` - `schema` - `initialInput` - `validate` - `revalidate` ##### Explanation The `FormConfig` object is passed to `useForm` with a required `schema` property and optional properties for `initialInput`, `validate`, and `revalidate` to control form behavior and validation timing. ### FormStore Form store interface that provides reactive access to the state of a form created with `useForm`. #### Generics - `TSchema` #### Definition - `FormStore` - `isSubmitting` - `isSubmitted` - `isValidating` - `isTouched` - `isDirty` - `isValid` - `errors` ##### Explanation The `errors` property only contains validation errors at the root level of the form. These are errors from the root object schema validation itself, not errors from nested fields. To retrieve all validation errors from all fields across the entire form, use the `getAllErrors` method instead. ### GetFieldErrorsConfig Configuration interface for retrieving field-specific errors. Used by the `getErrors` method when a specific field path is provided. #### Generics - `TSchema` - `TFieldPath` #### Definition - `GetFieldErrorsConfig` - `path` #### Related ##### Methods ### GetFieldInputConfig Configuration interface for retrieving field-specific input. Used by the `getInput` method when a specific field path is provided. #### Generics - `TSchema` - `TFieldPath` #### Definition - `GetFieldInputConfig` - `path` #### Related ##### Methods ### GetFormErrorsConfig Configuration interface for retrieving form-level errors. Used by the `getErrors` method when no specific field path is provided. #### Definition - `GetFormErrorsConfig` - `path` #### Related ##### Methods ### GetFormInputConfig Configuration interface for retrieving form-level input. Used by the `getInput` method when no specific field path is provided. #### Definition - `GetFormInputConfig` - `path` #### Related ##### Methods ### InsertConfig Configuration interface for inserting items into array fields. Used by the `insert` method. #### Generics - `TSchema` - `TFieldArrayPath` #### Definition - `InsertConfig` - `path` - `at` - `initialInput` #### Related ##### Methods ### PartialValues Utility type that recursively makes all value properties optional, including nested objects and arrays. #### Generics - `TValue` #### Definition - `PartialValues` #### Related ##### Methods ### Path Type definition for readonly arrays of path keys used to access nested properties. #### Definition - `Path` ### PathKey Type definition for keys used in paths to access nested properties in objects and arrays. #### Definition - `PathKey` ### MoveConfig Configuration interface for moving items within array fields. Used by the `move` method. #### Generics - `TSchema` - `TFieldArrayPath` #### Definition - `MoveConfig` - `path` - `from` - `to` #### Related ##### Methods ### RemoveConfig Configuration interface for removing items from array fields. Used by the `remove` method. #### Generics - `TSchema` - `TFieldArrayPath` #### Definition - `RemoveConfig` - `path` - `at` #### Related ##### Methods ### ReplaceConfig Configuration interface for replacing items in array fields. Used by the `replace` method. #### Generics - `TSchema` - `TFieldArrayPath` #### Definition - `ReplaceConfig` - `path` - `at` - `initialInput` #### Related ##### Methods ### RequiredPath A type that represents a required path into an object structure with at least one key. > This type is too complex to display. Please refer to the [source code](https://github.com/fabian-hiller/formisch/blob/main/packages/core/src/types/path.ts). #### Related ##### Methods ### ResetFieldConfig Configuration interface for resetting field-specific input and errors. Used by the `reset` method when a specific field path is provided. #### Generics - `TSchema` - `TFieldPath` #### Definition - `ResetFieldConfig` - `path` - `initialInput` - `keepErrors` - `keepInput` - `keepTouched` #### Related ##### Methods ### ResetFormConfig Configuration interface for resetting form input and errors. Used by the `reset` method when no specific field path is provided. #### Generics - `TSchema` #### Definition - `ResetFormConfig` - `initialInput` - `keepErrors` - `keepInput` - `keepSubmitted` - `keepTouched` #### Related ##### Methods ### Schema Schema type definition that represents any Valibot schema that can be used with Formisch. #### Definition - `Schema` #### Related ##### Primitives ##### Methods ### SetFieldErrorsConfig Configuration interface for setting field-specific errors. Used by the `setErrors` method when a specific field path is provided. #### Generics - `TSchema` - `TFieldPath` #### Definition - `SetFieldErrorsConfig` - `path` - `errors` #### Related ##### Methods ### SetFieldInputConfig Configuration interface for setting field-specific input. Used by the `setInput` method when a specific field path is provided. #### Generics - `TSchema` - `TFieldPath` #### Definition - `SetFieldInputConfig` - `path` - `input` #### Related ##### Methods ### SetFormErrorsConfig Configuration interface for setting form-level errors. Used by the `setErrors` method when no specific field path is provided. #### Definition - `SetFormErrorsConfig` - `path` - `errors` #### Related ##### Methods ### SetFormInputConfig Configuration interface for setting form-level input. Used by the `setInput` method when no specific field path is provided. #### Generics - `TSchema` #### Definition - `SetFormInputConfig` - `path` - `input` #### Related ##### Methods ### SubmitEvent Extended submit event interface for form submission. Extends the native `Event` interface with additional form-specific properties. #### Extends - `Event` #### Definition - `SubmitEvent` - `submitter` #### Related ##### Methods ##### Types ### SubmitHandler Function type for handling form submission. Used by the `handleSubmit` method. #### Generics - `TSchema` #### Parameters - `output` - `event` #### Related ##### Methods ##### Types ### SwapConfig Configuration interface for swapping items in array fields. Used by the `swap` method. #### Generics - `TSchema` - `TFieldArrayPath` #### Definition - `SwapConfig` - `path` - `at` - `and` #### Related ##### Methods ### UseFieldArrayConfig Configuration object for creating a field array store with `useFieldArray`. #### Generics - `TSchema` - `TFieldArrayPath` #### Definition - `UseFieldArrayConfig` - `path` ### UseFieldConfig Configuration object for creating a field store with `useField`. #### Generics - `TSchema` - `TFieldPath` #### Definition - `UseFieldConfig` - `path` ### ValidArrayPath Conditional type that returns the path if valid for array operations, otherwise the first possible valid array path based on the given value. #### Generics - `TValue` - `TPath` #### Definition - `ValidArrayPath` ### ValidateFormConfig Configuration interface for form validation. Used by the `validate` method to specify validation behavior. #### Definition - `ValidateFormConfig` - `shouldFocus` #### Related ##### Methods ### ValidationMode Validation mode type that defines when and how form validation is triggered. #### Definition - `ValidationMode` #### Related ##### Primitives ##### Methods