-
Hi everyone. I have a form that creates a product for an online store. The product has certain fields, such as - title, description, photo. There is also a field that assigns the product to a certain category, and depending on which category the product is assigned to, the product has additional, dynamic fields based on options that come from the server. For example, if I select category 1 in the form, the product will have an option for color where I can select a specific color, and if I select category 2, the form should have a size field where I can select a size. Here is how it looks like in the code
Here is the base component of the form
Now I want to add fields to it based on which categories are selected in categoryList
But the problem is that my original schema doesn't know anything about dynamic fields and I don't understand how to implement what I described. Here is an example of how optionSelectOptionList looks like
|
Beta Was this translation helpful? Give feedback.
Replies: 8 comments 3 replies
-
Perhaps this would help: |
Beta Was this translation helpful? Give feedback.
-
Thank you for your response. But I still don't see how it can help my situation. I have a strictly defined scheme
But a props can be passed to a form component, for example this one optionSelectOptionList
In that case, I need to generate a new schema, something like this
That is, fields should be added, based on the object obtained from the props, which fields of the object will be, how many of them will be - it is unknown, only the interface is known
Maybe I'm trying to solve the problem in the wrong way.
But in the merged schema I get this type and there is nothing about my original fields from the primary schema.
I must be doing something wrong, because it looks pretty complicated and I'm still not getting the results I want. Maybe you can advise me how to better realize my task? |
Beta Was this translation helpful? Give feedback.
-
I'm not really sure what you are trying to do. Perhaps you could simplify it? |
Beta Was this translation helpful? Give feedback.
-
Can you just make the extra properties optional? const productFormSchema = z.object( {
name: z.string().min( 3 ).max( 30 ).trim(),
description: z.string(),
about: z.string(),
img: z.string().array(),
categoryList: z.object( { id: z.string() } ).array(),
New: z.object( { id: z.string() } ).optional(),
Size: z.object( { id: z.string() } ).optional(),
Color: z.object( { id: z.string() } ).array().optional(),
} )
type ProductForm = z.input<typeof productFormSchema>
// type ProductForm = {
// name: string
// description: string
// about: string
// img: string[]
// categoryList: { id: string }[]
// New?: { id: string } | undefined
// Size?: { id: string } | undefined
// Color?: { id: string }[] | undefined
// } If you found my answer satisfactory, please consider supporting me. Even a small amount is greatly appreciated. Thanks friend! 🙏 |
Beta Was this translation helpful? Give feedback.
-
Hi, thank you for your reply. No, I can't make the fields optional because those fields are unspecified. They are created, deleted, renamed, at one point in time it could be Size, Color, and at another point in time it could be One, Two, Threee, Ololo, AnyFieldName, More, ...., Thousand. |
Beta Was this translation helpful? Give feedback.
-
Would you like this issue to remain open as a feature request? Or can I move it to discussions as a Q&A? |
Beta Was this translation helpful? Give feedback.
-
So far, I've solved my problem this way. In a schema that contains fields that are precisely defined, I added the propertyList field
Next I create a schema for the dynamic fields, based on the data
And I create a final schema with all the fields in it
In this case, I get the correct types for the defined fields and can work with the dynamic fields created based on the received data
|
Beta Was this translation helpful? Give feedback.
-
I did this
helpers.tsx
|
Beta Was this translation helpful? Give feedback.
So far, I've solved my problem this way. In a schema that contains fields that are precisely defined, I added the propertyList field
Next I create a schema for the dynamic fields, based on the data