N
Nuxt3mo ago
Gregor

nuxt ui with zod-validation

Hi, I have a form with this structure
const formData = ref({
email: "",
address: {
firstName: "",
lastName: "",
street: "",
addressDetails: "",
city: "",
country: "",
postalcode: "",
}
});
const schema = z.object({
email: z
.string()
.min(1, { message: "E-Mail is required" })
.email("Invalid email"),
address: z.object({
firstName: z.string().min(1, { message: "First Name is required" }),
sameBillingAddress: z.enum(["true", "false"]).array(),
lastName: z.string().min(1, { message: "Last Name is required" }),
street: z.string().min(1, { message: "Street is required" }),
city: z.string().min(1, { message: "City is required" }),
country: z.string().min(1, { message: "Country is required" }),
postalcode: z.string().min(1, { message: "Postalcode is required" }),
}),
shipping: z.enum(["standard", "express"], {
errorMap: () => ({ message: "Shipping is required" }),
}),
});
const formData = ref({
email: "",
address: {
firstName: "",
lastName: "",
street: "",
addressDetails: "",
city: "",
country: "",
postalcode: "",
}
});
const schema = z.object({
email: z
.string()
.min(1, { message: "E-Mail is required" })
.email("Invalid email"),
address: z.object({
firstName: z.string().min(1, { message: "First Name is required" }),
sameBillingAddress: z.enum(["true", "false"]).array(),
lastName: z.string().min(1, { message: "Last Name is required" }),
street: z.string().min(1, { message: "Street is required" }),
city: z.string().min(1, { message: "City is required" }),
country: z.string().min(1, { message: "Country is required" }),
postalcode: z.string().min(1, { message: "Postalcode is required" }),
}),
shipping: z.enum(["standard", "express"], {
errorMap: () => ({ message: "Shipping is required" }),
}),
});
<div class="flex-1 flex flex-col gap-10">
<UForm
:state="formData"
:schema="schema"
class="space-y-8 w-full flex flex-col"
@submit="onSubmit"
>
<checkoutAccount v-model="formData.email" />
<formsAddress
v-model:firstName="formData.address.firstName"
v-model:lastName="formData.address.lastName"
v-model:street="formData.address.street"
v-model:addressDetails="formData.address.addressDetails"
v-model:city="formData.address.city"
v-model:postalcode="formData.address.postalcode"
v-model:country="formData.address.country"
/>
<div class="flex-1 flex flex-col gap-10">
<UForm
:state="formData"
:schema="schema"
class="space-y-8 w-full flex flex-col"
@submit="onSubmit"
>
<checkoutAccount v-model="formData.email" />
<formsAddress
v-model:firstName="formData.address.firstName"
v-model:lastName="formData.address.lastName"
v-model:street="formData.address.street"
v-model:addressDetails="formData.address.addressDetails"
v-model:city="formData.address.city"
v-model:postalcode="formData.address.postalcode"
v-model:country="formData.address.country"
/>
if I don`t group the address inside an own object it works. The e-mail is validated correctly What do I have to do to get it up and running?
1 Reply
Gregor
GregorOP3mo ago
StackBlitz
Nuxt UI - Playground (forked) - StackBlitz
Playground of Nuxt UI, a UI library powered by Headless UI and Tailwind CSS for Nuxt.
Want results from more Discord servers?
Add your server