Mỗi ví dụ là một file Vue hoàn chỉnh. Chép vào ứng dụng đã cài registry, hoặc mở trong StackBlitz. Nhấn nút khi các ô còn trống để xem lỗi.
Ô nhập theo schema
Một schema zod giữ các quy tắc và thông báo. FormControl tự nối nhãn, chữ hướng dẫn và lỗi với field.
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'
// Thông báo lỗi nói sai ở đâu và cách sửa. Bấm "Thêm cửa hàng" khi các ô còn trống để xem.
const formSchema = toTypedSchema(z.object({
storeName: z.string({ required_error: 'Nhập tên cửa hàng.' }).trim().min(2, 'Nhập ít nhất 2 ký tự.'),
email: z.string({ required_error: 'Nhập địa chỉ email.' }).email('Nhập địa chỉ email như [email protected].'),
}))
const { handleSubmit } = useForm({ validationSchema: formSchema })
const saved = ref('')
const onSubmit = handleSubmit((values) => {
saved.value = `Đã thêm ${values.storeName}.`
})
</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>Tên cửa hàng</FormLabel>
<FormControl>
<Input autocomplete="organization" v-bind="componentField" />
</FormControl>
<FormDescription>Hiện trên hóa đơn và trong email gửi khách hàng.</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="email">
<FormItem>
<FormLabel>Email liên hệ</FormLabel>
<FormControl>
<Input type="email" autocomplete="email" v-bind="componentField" />
</FormControl>
<FormDescription>Cảnh báo đồng bộ của cửa hàng này được gửi về đây.</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<div class="flex gap-(--bh-space-100)">
<Button type="submit">Thêm cửa hàng</Button>
</div>
<p role="status" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">{{ saved }}</p>
</form>
</template>Select và ghi chú không bắt buộc
Cùng cách làm với native select và text area. Field không bắt buộc ghi “(không bắt buộc)” trong nhãn.
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: 'Chọn tần suất đồng bộ.' }),
note: z.string().max(200, 'Viết ghi chú tối đa 200 ký tự.').optional(),
}))
const { handleSubmit } = useForm({ validationSchema: formSchema })
const saved = ref('')
const onSubmit = handleSubmit(() => {
saved.value = 'Đã lưu lịch đồng bộ.'
})
</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>Đồng bộ tồn kho</FormLabel>
<FormControl>
<NativeSelect class="w-full" v-bind="componentField">
<NativeSelectOption value="" disabled>Chọn tần suất</NativeSelectOption>
<NativeSelectOption value="15m">Mỗi 15 phút</NativeSelectOption>
<NativeSelectOption value="1h">Mỗi giờ</NativeSelectOption>
<NativeSelectOption value="1d">Mỗi ngày một lần</NativeSelectOption>
</NativeSelect>
</FormControl>
<FormDescription>Từ ERP tới mọi kênh bán hàng.</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot="{ componentField }" name="note">
<FormItem>
<FormLabel>Ghi chú cho nhóm của bạn (không bắt buộc)</FormLabel>
<FormControl>
<Textarea v-bind="componentField" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<div class="flex gap-(--bh-space-100)">
<Button type="submit">Lưu lịch</Button>
</div>
<p role="status" class="m-0 text-(--bh-color-text-subtle) [font:var(--bh-font-body-small)]">{{ saved }}</p>
</form>
</template>