Coder Social home page Coder Social logo

Comments (3)

danielweinmann avatar danielweinmann commented on May 22, 2024 1

Hey, @gederer! Can you share a complete example of how you made it work with us? I'm designing an API to deal with nested objects and arrays, and it would be great to contemplate your use case when thinking about it.

from remix-forms.

danielweinmann avatar danielweinmann commented on May 22, 2024

Hey, @02JanDal! Thanks for creating this issue 💪

We don't have support for objects and arrays in the schema yet, but it is high on our priority list to add it :)

In the meantime, you can still use Remix Forms for the schema above, but instead of rendering a Field component, you'll need to render a custom UI directly inside of Form's children, with the help of register and other react-hook-form props. I haven't tested it yet, but I think all the client-side validations will work correctly that way.

The server-side validations do work with objects and arrays already, so you're covered on that end.

Can you please let us know if this works?

from remix-forms.

gederer avatar gederer commented on May 22, 2024

Hey, @danielweinmann. I tried this with a set off checkboxes, all with the same name. This is my Zod schema:

const schema = z.object({
  name: z.string().nonempty(),
  age: z.number(),
  hobbies: z.array(z.string()).nonempty("Must have at least one hobby")
});

When I have two or more checkboxes checked, I get the correct values passed into my mutation function. But, if I have only one checkbox checked, the form doesn't submit. I (strongly!) suspect this is because a single value is passed as a scalar, rather than as an array. Do you know how I might patch this up?

Update:

Changed my Zod schema to:

const schema = z.object({
  name: z.string().nonempty(),
  age: z.number(),
  hobbies: z.union([z.string().optional(), z.array(z.string()).optional()]).optional()
    .transform(val => typeof val !== "undefined" ? Array.isArray(val) ? val : [val] : [])
});

Now, it submits correctly with a single checkbox or multiple checkboxes checked. But, when I submit with zero checkboxes checked, it focuses (but does not check) the first checkbox without submitting. Oddly, if I submit a second time after the first checkbox is focused, it does submit correctly. Not sure why.

Update 2 (Finally got it):

const schema = z.object({
  name: z.string().nonempty(),
  age: z.number(),
  hobbies:
    z.preprocess(
      val => !!val ? Array.isArray(val) ? val : [val] : [],
      z.array(z.string())
    )
});

from remix-forms.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.