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.

setError doesn't prevent form submission if set outside of onChange handler

See original GitHub issue

When 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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
bluebill1049commented, Aug 17, 2019

looks like this is the correct behavior. here is the reason:

you have set an error during useEffect

useEffect(() => {
    setError("username", "notMatch", "please choose a different username");
  }, []);

however, your input validation rule is saying a different story:

         <input
            name="username"
            placeholder="username"
            defaultValue="lalala"
            ref={register({ required: true })}
          />

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:

         <input
            name="username"
            placeholder="username"
            defaultValue="lalala"
            ref={register({ required: true, validate: () => { // compare password using watch or other methods } })}
          />

server-side

const onSubmit = aysnc () => {
  if (await validateServer) {
    setError('username', 'servererror', 'messsage')
  }
}

hope this makes sense.

1reaction
GProstcommented, Aug 18, 2019

@bluebill1049 this does make sense to me indeed. However, I faced with one inconvenience… I want to achieve the following UX:

  1. The input doesn’t show an error until blur event (mode: 'onBlur' solves it)
  2. After an error is shown input gets validated on change event so that it hides an error as soon as the validation passes (similarly to what happens with mode: 'onSubmit')

But looks like I can’t currently achieve that (not easily at least).

Read more comments on GitHub >

github_iconTop 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 >

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