so you have a big form that has productsVariantsSchema as its structure.
You want to split off parts of the form (the variant) and generate it once per item in the array.
The way to do that would be using withForm in Form Composition
You would have a structure like:
const defaultValues: z.input<typeof productSchema> = { };
const sharedFormOptions = formOptions({
defaultValues,
validators: {
onChange: productSchema
}
})
const VariantForm = withForm({
...sharedFormOptions,
props: {
index: 0
},
render: function Render({ form, index }) {
const namespace = `productVariants[${index}]` as const;
return <form.AppField name={`${namespace}.slug`} /* ... */ />
}
})
const form = useAppForm({
...sharedFormOptions,
defaultValues: fromQuery.data,
onSubmit: ({ value }) => { }
})
<form.AppField name="productVariants" mode="array">
{field => field.state.value.map((variant, index) => <VariantForm form={form} index={index} />)
</form.AppField>