Vue3 composables for handling form submit with optional valibot integration
npm install vue-valibotA set of simple Vue3 composables for handling form submit, with optional valibot integration.
Unlike FormKit, VeeValidate and others, keeps things simple and doesn't interfere with neither data storage nor the UI workflow.
Full Typescript support with type inference.
``sh`
npm install vue-valibot
`vue
useForm composable
`ts
const {
// All return values are optional to use.
form,
submit,
submitting,
submitted,
errors,
} = useForm({
// All options are optional to provide.
input,
schema,
submit,
onErrors,
// Optional defaults for the return values.
form,
submitting,
submitted,
errors,
})
`useForm options
$3
(Optional) Input value, or ref, or a getter, of the data to be validated and/or passed to
submit.$3
(Optional) Valibot schema (or a function that returns a schema, such as when the schema depends on the context).
$3
(Optional) Form submit callback.
Only called if:
- Form is not being submitted at the moment (
submitting.value is falsy).
- HTML5 validation passes (if enabled).
- Valibot validation passes (if used).If
input and/or schema were provided, the first argument passed to the submit callback is the (possibly validated) form input. The rest of the arguments are the submit function arguments.During execution,
submitting is true.
After successfull execution, submitted is true.$3
(Optional) Error callback.
Called (and awaited) if the validation fails, or if
errors.value was set by the submit handler.$3
Normally,
useForm will create and return these refs (see below), but you may optionally provide your own.For example, this could be used to share the single
submitting flag between multiple forms:`ts
const submitting = ref(false)const { submit: submit1 } = useForm({
submitting,
async submit() { / ... / }
})
const { submit: submit2 } = useForm({
submitting,
async submit() { / ... / }
})
//
submitting will be true during submit of either form.
`useForm shortcut
All the composable options are optional. If the only option you need is
submit, there is a shortcut variant:`ts
const { submit, submitting } = useForm(async () => {
// submitting is true during this callback.
await api.post()
})
`useForm return
$3
The form element ref.
Using it with