PXD
v0.2.1-dev

useForm

Headless form controller behind PForm. Use it when the component tree does not fit, or when the form owns no markup.

Exports

import { useForm } from 'pxd'

Types

import type {
  FormError,
  FormPath,
  FormValidateOn,
  FormValidatePayload,
  StandardSchema,
  UseFormOptions,
  UseFormReturn,
} from 'pxd'

Params

NameTypeDefaultDescription
modelMaybeRefOrGetter<Record<string, any>>-The caller’s reactive object
schemaMaybeRefOrGetter<StandardSchema>-Any Standard Schema
validate(model) => FormError[] | Promise<FormError[]>-Custom validation, runs after the schema
initial-valuesMaybeRefOrGetter<Partial<M>>current modelSnapshot used by reset()
validate-on('blur' | 'change')[]['blur', 'change']Change validation trigger
debouncenumber300Debounce for change validation in milliseconds
on-validate(payload: FormValidatePayload) => void-Called after each validation run
const model = reactive({ email: '' })

const form = useForm({
  model,
  validate: (values) =>
    values.email ? [] : [{ name: 'email', message: 'Email is required' }],
})

async function submit() {
  const result = await form.submit()

  if (result.valid) {
    console.log(result.data)
  }
}

Source

Source