Skip to content

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/zod
bash
npm install vee-validate zod @vee-validate/zod
bash
yarn add vee-validate zod @vee-validate/zod
bash
bun add vee-validate zod @vee-validate/zod

Props

Form

PropTypeDefaultDescription
validation-schemaSchema-A Zod schema for form validation
keep-valuesbooleanfalseWhether to keep values after form unmounts

FormField

PropTypeDefaultDescription
namestring-The name of the field
rulesRuleExpression-Validation rules for the field

Slots

Form

SlotDescription
defaultThe form content

FormField

SlotDescription
defaultSlot for the field component, receives componentField binding