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.

Submit handler won't fire even after validations pass

See original GitHub issue

Describe the bug In a form with a validate prop set on register, the submit handler won’t fire after the user fixes a validation error.

To Reproduce Steps to reproduce the behavior:

  1. Go to https://codesandbox.io/s/smoosh-sunset-t8q22?file=/src/App.js
  2. Enter some numeric value for the Count 1 field, and then some numeric value for the Weight 1 field, and see the form getting submitted (this is expected).
  3. Now refresh the page, and on refresh, press submit without entering anything. Notice validation failures in the console (this is expected).
  4. Now without refreshing, enter some numeric value for Count 1, and some numeric value for Count 2, and click on Submit.
  5. Notice that validation succeeds (see console, and this is expected) but the form’s submit handler won’t fire. This is the problem, as I’d expect the form to submit now that all validations pass.

Codesandbox link https://codesandbox.io/s/smoosh-sunset-t8q22?file=/src/App.js

Expected behavior On submitting the form after all validation errors are fixed, the submit handler should be fired.

Desktop:

  • OS: macOS Mojave 10.14.6
  • Browser Chrome 85.0.4183.16 (Official Build) dev (64-bit)
  • Version react-hook-form v6.0.4

Additional context Might be somewhat related to https://github.com/react-hook-form/react-hook-form/issues/2180, but I’m not a 100% sure.

Quick note about the validation logic

  1. atLeast1Count - The user is expected to enter input for at least one of the ‘Count’ fields.
  2. requiredWeight - For every ‘Count’ field that has a value > 0, the corresponding ‘Weight’ field should also have a value.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
paambaaticommented, Jul 14, 2020

1reaction
paambaaticommented, Jul 14, 2020

@bluebill1049 Thanks for the CSB, I can see it works. But I made a fork of it to add some of my changes (only in validation), and it still doesn’t work 😓.

See https://codesandbox.io/s/icy-snowflake-3dzgo?file=/src/ComplexInput.jsx

Steps to reproduce

  1. Submit form without any inputs. Notice it won’t submit (expected, as validation fails).
  2. Now enter any input in Count 1 field.
  3. Then enter any input in Count 2 field.
  4. Now submit form. See that both validations pass (notice console), but the form still won’t submit.

Differences from your CSB

  1. Added validation logic that tests for -

    a. At least 1 count field must be entered. b. For every count field that has a value, the corresponding weight field must have a value too.

So the key difference in my change is that validations dynamically depend on getValues().

What is confusing for me is that both validations fail and pass correctly based on inputs, but the form won’t submit when validation has failed at least once. On a refreshed page with all inputs entered correctly, the form submits without any issues.

Read more comments on GitHub >

github_iconTop Results From Across the Web

submitHandler is not triggered although it is valid
Now I want to validate the form before submitting but I cannot make it work. The submitHandler option is not called even though...
Read more >
Angular Reactive Forms: trigger validation on submit
In this article we will learn different approaches of validating all form fields when user clicks on submit button for Angular Reactive ...
Read more >
HTMLFormElement: submit event - Web APIs | MDN
Note: Trying to submit a form that does not pass validation triggers an invalid event. In this case, the validation prevents form submission ......
Read more >
Submit Validation Example - Redux Form
handleSubmit} inside your decorated component to cause it to fire when the submit button is clicked. Pass it as a parameter to the...
Read more >
submitForm() is not called when custom form #submit handler ...
In Drupal 8 - when I do this the core submit and validation ... and was confirmed this should still work like D7,...
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