Appearance
Form
Building forms with validation and error handling using VeeValidate and Zod.
Examples
Default
vue
<script setup>
import { useForm } from 'vee-validate'
import { toTypedSchema } from '@vee-validate/zod'
import * as z from 'zod'
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
Input,
Button
} from '@empathyds/vue'
const formSchema = toTypedSchema(
z.object({
username: z.string().min(2, {
message: 'Username must be at least 2 characters.',
}),
})
)
function onSubmit(values) {
console.log('Form submitted:', values)
}
</script>
<template>
<Form :validation-schema="formSchema" @submit="onSubmit">
<FormField v-slot="{ componentField }" name="username">
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input type="text" placeholder="shadcn" v-bind="componentField" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<Button type="submit" class="mt-4">Submit</Button>
</Form>
</template>Installation
bash
pnpm add vee-validate zod @vee-validate/zodbash
npm install vee-validate zod @vee-validate/zodbash
yarn add vee-validate zod @vee-validate/zodbash
bun add vee-validate zod @vee-validate/zodProps
Form
| Prop | Type | Default | Description |
|---|---|---|---|
| validation-schema | Schema | - | A Zod schema for form validation |
| keep-values | boolean | false | Whether to keep values after form unmounts |
FormField
| Prop | Type | Default | Description |
|---|---|---|---|
| name | string | - | The name of the field |
| rules | RuleExpression | - | Validation rules for the field |
Slots
Form
| Slot | Description |
|---|---|
| default | The form content |
FormField
| Slot | Description |
|---|---|
| default | Slot for the field component, receives componentField binding |
