question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to prevent submit in nested form

See original GitHub issue

I have a nested form that looks like this

//this is the form
<form>

// this for quick add functionality and display the data from the form above
<form>
</form>

</form>

The problem I have is when the children submit the data, it also trigger the submit from the parent, is there any way to prevent this from happening. P/S: also, how to trigger submit form <button>, something like <button onClick={() => handleSubmit()}>submit</button>

Thanks

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (4 by maintainers)

github_iconTop GitHub Comments

30reactions
vikas26294commented, May 8, 2020

I also ran into same issue. I was able to solve it by modifying my submit function to

  const onSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
    // this part is for stopping parent forms to trigger their submit
    if (event) {
      // sometimes not true, e.g. React Native
      if (typeof event.preventDefault === 'function') {
        event.preventDefault();
      }
      if (typeof event.stopPropagation === 'function') {
        // prevent any outer forms from receiving the event too
        event.stopPropagation();
      }
    }

    return formHandlers.handleSubmit(async (values) => {
       // your code can go here
    })(event);
  };
11reactions
gaozhidfcommented, May 29, 2020

e.stopPropagation() can stop submit from nested form

<Form>
  <Form onSubmit={e => {
    e.preventDefault();
    // your code
    e.stopPropagation();
  }} >
  </Form>
</Form>
Read more comments on GitHub >

github_iconTop Results From Across the Web

Prevent form inside a form from being submitted - Stack Overflow
Forms should not be nested. The behaviour of such invalid markup is not specified. You should fix the markup. If, for some reason,...
Read more >
Preventing form submission with dynamic nested forms : r/rails
Preventing form submission with dynamic nested forms. I have a set up where users can add answer options to quizzes with nested forms....
Read more >
react hook form nested | The AI Search Engine You Control
React Hook Form: Submit a form with nested components or extract fields of ... You could use the hook useFormContext to avoid to...
Read more >
Nested Forms - Apache Software Foundation
It might be a good idea to use input instead of button elements in your form hierarchy. IE tends to send all button...
Read more >
InDepth Guide for Creating Nested and Reusable Forms
Introduction; How to create and use FormGroup; How to create nested form-groups ... <button type="submit" [disabled]="profileFormGroup.invalid"> Submit form ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found