0

OBS: I decided to open a answered question here because I have seen so many posts in Reddit and Github about submitting a form with shadcnui/react-hook-form in a Remix app.

I had an issue building a form with Remix and shadcn/ui (which uses react-hook-form) and I just solved it after hours.

To build the form, I followed these pages:

The problem was happening when I was submitting the form, not receiving any logs that I put in some sections of my code.

I checked other cases and tried some things to resolve it:

  • changing form onSubmit attribute to various structures
  • verified if some fields were not "registered" in form (I added the UI components, that are registered by itself)
  • verified any validation errors that could not be showed in screen.
  • added field values (from render attribute in my Switch component from shadcn, like that:
<Switch
  checked={field.value}
  onCheckedChange={field.onChange}
  name={field.name}
  ref={field.ref}
  disabled={field.disabled}
  onBlur={field.onBlur}
/>

1 Answer 1

1

To resolve my problem, I changed the onSubmit of my Form element:

        <Form
          id="settings-form"
          onSubmit={
            form.handleSubmit((data) => {
              submit(data, { method: 'post', encType: 'application/json' });
            }, (errors) => console.error(errors))
          }
        >

I also needed to change the action function, getting the object instead of FormData: await request.json().

At first, I tried to get as FormData using:

  • submit({ myKey: "myValue" }, { method: "post" }); (docs page says that will be serialized as FormData)
  • multipart/form-data and application/x-www-form-urlencoded as encType, but I always got an empty FormData in action function.

To be honest, I did not understand why some solutions didn't work. That's how I solved it, but maybe there is a better approach, like the remix-hook-form package that I didn't want to use for particular reasons (but it looks nice).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.