Form
Composable, validated form fields built on vee-validate with labels and error messages.
Installation
npx shadcn-vue@latest add https://vuedocs.canceydejean.dev/r/form.jsonUsage
Use these primitives to build accessible forms with per-field validation state — FormField
(re-exported from vee-validate) drives each field's context, and FormLabel/FormMessage read
error and id state from it automatically via useFormField. Pair with @vee-validate/zod for
schema validation.
<script setup lang="ts">
import { toTypedSchema } from "@vee-validate/zod";
import { z } from "zod";
import { Button } from "@/components/ui/button";
import {
Form,
FormControl,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@/components/ui/form";
import { Input } from "@/components/ui/input";
const formSchema = toTypedSchema(
z.object({
username: z.string().min(2, "Username must be at least 2 characters."),
}),
);
function onSubmit(values: Record<string, unknown>) {
console.log(values);
}
</script>
<template>
<Form :validation-schema="formSchema" class="space-y-6" @submit="onSubmit">
<FormField v-slot="{ componentField }" name="username">
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="shadcn" v-bind="componentField" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<Button type="submit">Submit</Button>
</Form>
</template>