Skip to content

[Remix+Zod] How to deal with a user-managed array of objects? #288

Answered by edmundhung
halljus asked this question in Q&A
Discussion options

You must be logged in to vote

I guess your are dealing with multiple select like dropdown that managing the items here. Since this wont be supported without JS anyway, it's fine to just pass the array to the server as a JSON:

// Assume you are managing the state in useState()
const [items, setItems] = useState(...)

// Just send the array to the server as a JSON
<input type="hidden" name={fields.items} value={JSON.stringify(items)} />

// Then parse the JSON in your zod schema through a preprocess
const schema = z.object({
  // ...
  items: z.preprocess(
    value => JSON.parse(value),
    z
      .object({
        title: z.string({ required_error: 'Title is required' }),
        description: z.string({ required_error: 

Replies: 1 comment 4 replies

Comment options

You must be logged in to vote
4 replies
@halljus
Comment options

@adaboese
Comment options

@adaboese
Comment options

@edmundhung
Comment options

Answer selected by halljus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants