forge42dev / remix-hook-form

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

submitData string issue #54

Closed CripyIce closed 9 months ago

CripyIce commented 10 months ago

Since 3.2.0 all of my forms that has submitData passes strings with quotes. Example:

  const {
    handleSubmit,
    formState: { errors },
    register,
    setValue,
  } = useRemixForm({
    mode: "onSubmit",
    defaultValues: {
      title: currentCourse.title ?? "",
      desc: currentCourse.desc ?? "",
    },
    submitData: {
      submitAction: "SAVE",
    },
    resolver,
  });
image

Downgrading to version 3.1.0 make it work as it should:

image
AlemTuzlak commented 9 months ago

Hmm so the reason this happens is because in order to preserve your types and values instead of just values i JSON.stringify() each value so that later on on the server I can preserve numbers and booleans and so on. This is in line with what I was doing before v3 where I just JSON.stringified an object and sent it over the line instead. I think I can create a flag that allows you to either stringify everything or not based on your preference

CripyIce commented 9 months ago

As of the current version, how should I parse the values correctly?

AlemTuzlak commented 9 months ago

parseFormData utility will return it to you properly, until I figure out a way to let you choose your use case, otherwise if you need something more custom you can either use your own submitter, something like: onValid: data => submit(...your way to submit...) or you can just JSON.parse() every field on the backend

CripyIce commented 9 months ago

Well, JSON.parse() did help for now. If I will be able to configure this behavior with a flag it would be nice! Thanks!

AlemTuzlak commented 9 months ago

@CripyIce I released a new release that lets you pass a prop into useForm that allows you to either stringify strings as well or send them as is!