NOTFORM
Composables

useNotForm

The main composable for initializing and managing form state and validation.

useNotForm is the heart of notform . It initializes the form instance with validation logic, reactive state, and lifecycle methods.

Usage

form.vue
<script lang="ts" setup>
import { useNotForm } from 'notform'
import { z } from 'zod'

const { state, id, submit } = useNotForm({
  schema: z.object({
    name: z.string().min(2)
  }),
  onSubmit: async (data) => {
    console.log(data)
  }
})
</script>

API

Options

The useNotForm composable accepts a configuration object as its first argument.

OptionTypeDefaultDescription
idstringuseId()Unique form identifier.
schemaMaybeRefOrGetter<TSchema>RequiredThe validation schema (Zod, Valibot, etc.).
initialStateDeepPartial<Input>{}Initial values for the form fields.
initialErrorsIssue[][]Initial validation issues to display.
mode'lazy' | 'eager''eager'Validation behavioral strategy.
validateOnValidationTriggers[]['blur', 'input', 'change']Events that trigger field validation.
onValidate(data: StandardSchemaV1.InferOutput<TSchema>) => boolean | void | StandardSchemaV1.Issue[] | Promise<boolean | void | StandardSchemaV1.Issue[]>-Custom validation logic that runs after schema validation
onReset() => void-Callback triggered when the form is reset.
onError(errors: StandardSchemaV1.Issue[]) => void-Callback triggered when validation fails.
onSubmit(data: StandardSchemaV1.InferOutput<TSchema>) => void | Promise<void>-Callback triggered when validation passes and the form is submitted.

Returns

useNotForm returns a form context object containing reactive state and methods.

State

PropertyTypeDescription
idstringUnique form identifier.
stateRef<Input>Deeply reactive object of field values.
errorsRef<Issue[]>Reactive array of active validation issues.
isValidatingRef<boolean>Reactive status of the async validation process.
isSubmittingRef<boolean>Reactive status of form submission.
touchedFieldsRef<Set<string>>Reactive set of touched field paths.
dirtyFieldsRef<Set<string>>Reactive set of dirty field paths.
isValidComputedRef<boolean>Whether the entire form is valid.
isTouchedComputedRef<boolean>Whether any field has been interacted with.
isDirtyComputedRef<boolean>Whether any field has been modified.

Methods

submit(event: Event)

Validates the entire form and then triggers the onSubmit callback if successful. Marks all fields as touched and dirty.

validate()

Manually triggers form-wide validation. Returns a promise with the validation result.

validateField(path: string)

Manually triggers validation for a specific field path.

reset(state?, errors?, validate?)

Reverts form state and errors to their initial baseline or custom values.

  • state: Optional partial state to reset to.
  • errors: Optional issues list to reset to.
  • validate: Whether to re-validate after reset (default: false).

setState(state, validate?)

Updates the form state with the provided partial object.

  • validate: Whether to re-validate after update (default: true).

setErrors(errors)

Directly sets the form errors.

clearErrors()

Removes all active validation messages.

getFieldErrors(field: string)

Returns the list of validation issues for a specific field path.

touchField(field: string)

Marks a specific field as touched.

touchAllFields()

Marks all fields in the form as touched.

dirtyField(field: string)

Marks a specific field as dirty.

dirtyAllFields()

Marks all fields in the form as dirty.