0

I have a form with multiple components and tried to follow steps in this tutorial, to have my inputs validated: 7 Form Components For React Hook Form I Always Use

I just tried with first one, which is name, but validation won't happen. Instead console shoots a message: form submission canceled because the form is not connected react

Here is my sandbox: Sandbox

The form component should manage the overall validations FormProvider, useFormon submit, while in my test component I tried to manage the single input validations and error messages. It is important however that Form.js submit button keeps the onClick={makeContract}

1 Answer 1

1

You need to remove your onClick handler in your <input type="submit">

In react-hook-form, you should have only one submit handler, which you already had here

<form onSubmit={handleSubmit(onSubmit)}

the onSubmit handler here will only be called if all the fields in your form are valid.

So if you want to call makeContract when submitting form, call it inside onSubmit

  // only called when all fields valid
  const onSubmit = (data) => {
    console.log(data);
    makeContract();
  };

Live example

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.