Every example is a complete Vue file. Copy it into an app that has the registry installed, or open it in StackBlitz. Press the button with the fields empty to see the errors.
Text fields with a schema
A zod schema holds the rules and the messages. FormControl links the label, help text and error to the field for you.
FormConnectStore.vue
<script setup lang="ts">
import { toTypedSchema } from '@vee-validate/zod'
import { useForm } from 'vee-validate'
import { ref } from 'vue'
import { z } from 'zod'
import { Button } from '@/components/ui/button'
import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { Input } from '@/components/ui/input'
// Messages say what is wrong and how to fix it. Press "Add store" with the fields empty to see them.
const formSchema = toTypedSchema(z.object({
storeName: z.string({ required_error: 'Enter the store name.' }).trim().min(2, 'Enter at least 2 characters.'),
email: z.string({ required_error: 'Enter an email address.' }).email('Enter an email address like [email protected].'),
}))
const { handleSubmit } = useForm({ validationSchema: formSchema })
const saved = ref('')
const onSubmit = handleSubmit((values) => {
saved.value = `${values.storeName} was added.`
})
</script>
<template>
<form class="grid w-full max-w-sm gap-(--bh-space-stack)" novalidate @submit="onSubmit">
<FormField v-slot="{ componentField }" name="storeName">
<FormItem>
<FormLabel>Store name</FormLabel>
<FormControl>
<Input autocomplete="organization" v-bind="componentField" />
</FormControl>
<FormDescription>Shown on invoices and in emails to customers.</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="email">
<FormItem>
<FormLabel>Contact email</FormLabel>
<FormControl>
<Input type="email" autocomplete="email" v-bind="componentField" />
</FormControl>
<FormDescription>We send sync alerts for this store here.</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<div class="flex gap-(--bh-space-100)">
<Button type="submit">Add store</Button>
</div>
<p role="status" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">{{ saved }}</p>
</form>
</template>A select and an optional note
The same pattern with a native select and a text area. Optional fields say “(optional)” in the label.
FormSyncSchedule.vue
<script setup lang="ts">
import { toTypedSchema } from '@vee-validate/zod'
import { useForm } from 'vee-validate'
import { ref } from 'vue'
import { z } from 'zod'
import { Button } from '@/components/ui/button'
import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage } from '@/components/ui/form'
import { NativeSelect, NativeSelectOption } from '@/components/ui/native-select'
import { Textarea } from '@/components/ui/textarea'
const formSchema = toTypedSchema(z.object({
frequency: z.enum(['15m', '1h', '1d'], { required_error: 'Choose how often to sync.' }),
note: z.string().max(200, 'Keep the note to 200 characters or fewer.').optional(),
}))
const { handleSubmit } = useForm({ validationSchema: formSchema })
const saved = ref('')
const onSubmit = handleSubmit(() => {
saved.value = 'Sync schedule saved.'
})
</script>
<template>
<form class="grid w-full max-w-sm gap-(--bh-space-stack)" novalidate @submit="onSubmit">
<FormField v-slot="{ componentField }" name="frequency">
<FormItem>
<FormLabel>Sync inventory</FormLabel>
<FormControl>
<NativeSelect class="w-full" v-bind="componentField">
<NativeSelectOption value="" disabled>Choose how often</NativeSelectOption>
<NativeSelectOption value="15m">Every 15 minutes</NativeSelectOption>
<NativeSelectOption value="1h">Every hour</NativeSelectOption>
<NativeSelectOption value="1d">Once a day</NativeSelectOption>
</NativeSelect>
</FormControl>
<FormDescription>From the ERP to every sales channel.</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="note">
<FormItem>
<FormLabel>Note for your team (optional)</FormLabel>
<FormControl>
<Textarea v-bind="componentField" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<div class="flex gap-(--bh-space-100)">
<Button type="submit">Save schedule</Button>
</div>
<p role="status" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">{{ saved }}</p>
</form>
</template>