Submit handler won't fire even after validations pass
See original GitHub issueDescribe 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:
- Go to https://codesandbox.io/s/smoosh-sunset-t8q22?file=/src/App.js
- 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).
- Now refresh the page, and on refresh, press submit without entering anything. Notice validation failures in the console (this is expected).
- Now without refreshing, enter some numeric value for Count 1, and some numeric value for Count 2, and click on Submit.
- 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
atLeast1Count
- The user is expected to enter input for at least one of the ‘Count’ fields.requiredWeight
- For every ‘Count’ field that has a value > 0, the corresponding ‘Weight’ field should also have a value.
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@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
Differences from your CSB
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.