setError doesn't prevent form submission if set outside of onChange handler
See original GitHub issueWhen I try to set an error via setError API outside of onChange handler (in onBlur for example) it sets the error and the formState.isValid equals to false
. However, it doesn’t prevent form submission.
Is that by design?
Here is a codesandbox
Expected behavior:
When submit button is clicked handleSubmit
isn’t called and the problematic input gets focused.
P.S. Thank you for your great work on this library! 👏
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:5 (5 by maintainers)
Top Results From Across the Web
useForm - setError - React Hook Form
This method will force set isValid formState to false , however, it's important to aware isValid will always be derived by the validation...
Read more >React - Preventing Form Submission - Stack Overflow
In javascript you can prevent that by using an event handler and calling e.preventDefault() on button click, or form submit. e is the...
Read more >How to prevent form submit if an onChange script h...
Check all the validation there and if all the field values are valid then continue submit or if not valid then set error...
Read more >Handling Forms - VeeValidate
If you have a submit listener on the Form component, vee-validate assumes you will be handling submissions via JavaScript (AJAX) and automatically calls...
Read more >useField() - Formik
value?: string - Works only for inputs of type checkbox and radio . When a form is submitted, checkboxes and radios are submitted...
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
looks like this is the correct behavior. here is the reason:
you have set an error during
useEffect
however, your input validation rule is saying a different story:
which is only required.
During submission, react hook form will validate your validation rule in
register/schema
, and then validation rule meet and pass.which you should have done is put the validation inside of the register or during submit for server side validation:
server-side
hope this makes sense.
@bluebill1049 this does make sense to me indeed. However, I faced with one inconvenience… I want to achieve the following UX:
blur
event (mode: 'onBlur'
solves it)change
event so that it hides an error as soon as the validation passes (similarly to what happens withmode: 'onSubmit'
)But looks like I can’t currently achieve that (not easily at least).