Ability to scroll to the input with error

See original GitHub issue

It would be nice to have some scrollToError method which would scroll to the highest input found which name matches one of the errors keys.

Currently, there is a similar behavior that happens after the form validation but there are cases when errors are set manually for some fields using setError API, for example, after server’s error response.

I believe I could implement such functionality outside RHF but I’d have to store input refs and names separately for this and it just feels like it would be much better if it was implemented in RHF.

Or perhaps, alternatively, if I could access all input refs mapped to fields names from RHF it would make it simpler to implement such scrolling functionality manually.

Issue Analytics

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

github_iconTop GitHub Comments

9reactions
rkokcommented, Aug 29, 2021

Here’s one that scrolls to the top-most input:

const elements = Object.keys(errors).map(name => document.getElementsByName(name)[0]).filter(el => !!el);
elements.sort((a, b) => b.scrollHeight - a.scrollHeight);
elements[0]?.scrollIntoView({behavior: 'smooth', block: 'center'});
3reactions
eberens-nydigcommented, Jun 28, 2021

One thing I have noticed is errors are not in order they are registered but alphabetical. So the first ref that you scroll to may not be the top-most. I’m not sure how one would go about doing that.

function useScrollToError(errors) {
  useEffect(() => {
    for(const error of Object.values(errors)) {
      if(error?.ref) {
        error.ref.scrollIntoView({ behavior: 'smooth' })
        break
      }
    }
  }, [errors])
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to scroll to the first invalid control once a form has been ...
The solution to the problem was to scroll a document, once an invalid form has been submitted, so that the first invalid control...
Read more >
Scroll to the first input error of HTML5 form validation
I have multiple input fields in the form marked with required attribute. So when one of the first few input fields are left...
Read more >
Scroll to Input on Formik Failed Submission
The first step is to transform a Formik error object to dot-notation, so that we can later on search for inputs by their...
Read more >
Snagit Scrolling Capture Not Working for Previous Versions
Uncheck the Use fastest scrolling method option. If that does not work, increase the scroll delay. Click Apply. In Snagit, click the Save...
Read more >
scroll-margin - CSS: Cascading Style Sheets - MDN Web Docs
You can see the effect of scroll-margin by scrolling to a point partway between two of the "pages" of the example's content. The...
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