Я использую комбобокс от Shadcn-ui (https://www.shadcn-vue.com/docs/compents/combobox#combobox) в своем проекте Nuxt 3. Я не могу проверить свой список со списком с помощью Zod. Когда я выбираю элемент и отправляю форму, я всегда получаю сообщение об ошибке «Требуется учетная запись». Что бы я ни выбрал, проверка не проходит. Может кто-нибудь помочь мне найти проблему, пожалуйста?
Мой код:
<script setup lang = "ts">
const accounts = [
{ value: 'nuxt.js', label: 'Nuxt.js' },
{ value: 'remix', label: 'Remix' },
{ value: 'astro', label: 'Astro' },
]
const open = ref(false)
const value = ref('')
const formSchema = toTypedSchema(z.object({
accountSelected: z.string({
required_error: "Account is required"
}),
setupName: z.string({
required_error: "Setup is required"
})
}))
const form = useForm({
validationSchema: formSchema,
})
const submitForm = form.handleSubmit((values) => {
console.info("form submitted");
})
</script>
<template>
<form @submit = "submitForm">
<FormField v-slot = "{ componentField }" name = "accountSelected">
<FormItem class = "flex flex-col">
<FormLabel>Select account</FormLabel>
<FormControl>
<Popover v-model:open = "open">
<PopoverTrigger as-child>
<Button variant = "outline" role = "combobox" :aria-expanded = "open">
{{ value
? accounts.find((account) => account.value === value)?.label
: "Select account..." }}
<ChevronsUpDown class = "ml-2 h-4 w-4 shrink-0 opacity-50" />
</Button>
</PopoverTrigger>
<PopoverContent>
<Command>
<CommandInput placeholder = "Search account..." />
<CommandEmpty>No account found.</CommandEmpty>
<CommandList>
<CommandGroup>
<CommandItem v-bind = "componentField" v-for = "account in accounts"
:key = "account.value" :value = "account.value" @select = "(ev) => {
if (typeof ev.detail.value === 'string') {
value = ev.detail.value
}
open = false
}">
{{ account.label }}
<Check :class = "cn(
'ml-auto h-4 w-4',
value === account.value ? 'opacity-100' : 'opacity-0',
)" />
</CommandItem>
</CommandGroup>
</CommandList>
</Command>
</PopoverContent>
</Popover>
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<FormField v-slot = "{ componentField }" name = "setupName">
<FormItem>
<FormLabel>Name</FormLabel>
<FormControl>
<Input type = "text" v-bind = "componentField" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<Button type = "submit" class = "flex justify-center items-center">
Send
</Button>
</form>
</template>





Попробуйте установить значение вручную, как показано ниже:
@select = "(ev) => {
if (typeof ev.detail.value === 'string') {
value = ev.detail.value
form.setFieldValue('accountSelected', ev.detail.value)
}
open = false
}"