forge42dev / remix-hook-form

Open source wrapper for react-hook-form aimed at Remix.run
MIT License
330 stars 27 forks source link

Have to set stringifyAllValues: false else all strings are double quoted #77

Closed vrn-hkz closed 7 months ago

vrn-hkz commented 7 months ago

Because of this line in createFormData(data: T, stringifyAll = true) in ./utilities/index.ts

 if (stringifyAll) {
        formData.append(key, JSON.stringify(value)); // <---- This double quotes everything
      } else {
        formData.append(
          key,
          typeof value === "string" ? value : JSON.stringify(value),
        );
      }

So when accessing FormData in backend with formData.get('email') the result is "/"x@x.com/"" with double quoted string. What can be the workaround for this?

Currently I am setting stringifyAllValues: false and coercing all to strings in my zod schema.

AlemTuzlak commented 7 months ago

@vrn-hkz this is the intended behavior, if you're interested in why this is done you can find a detailed explanation here: https://github.com/react-hook-form/react-hook-form/pull/11061#issuecomment-1866564244 I stringify it so I can preserve the original types and easily parse back to original values, you can use the "false" option to do that yourself obviously but then you would have to convert everything back manually, eg string number => number, string boolean => boolean

vrn-hkz commented 7 months ago

Ok thanks for the feeback!