React-final-form: [Thoughts] focus first error

Created on 5 Dec 2017  路  8Comments  路  Source: final-form/react-final-form

Hi again.
Maybe you have any thoughts how to make scrolling to first error message using your validation library?

Most helpful comment

<FormSpy
  subscription={{ submitFailed: true }}
  onChange={() => {
    const el = document.querySelector('.error')
    if (el) {
        el.scrollIntoView({ behavior: 'smooth' })
    }
  }}
/>

All 8 comments

Well, the FormRenderProps given to your form render function _does_ have all the form errors. Actually scrolling them into view is up to you, I'm afraid.

Yes, I already found that methods, but what event is better to stick to? Where is the best way to scroll?
componentWillReceiveProps will give to much side effects, onSumbit won't be called when frontend validation fails, form validate function have different purposes than accessing DOM and scrolling.
maybe some hidden component events or calls?

@linashredinger have you figured out how to do this?

@circlingthesun, erikras sent me possible solution:

<FormSpy
  subscription={{ submitFailed: true }}
   onChange={(v) => {
    scrollIntoView(document.querySelector('.is-invalid-input'));
  }}
/>

@linashredinger @erikras that works great on the initial submit, however, if the user submits again, 'submitFailed' remains false and the formspy isn't called again. Any suggestions there?

@linashredinger: https://github.com/final-form/final-form-focus. Thanks @erikras!

<FormSpy
  subscription={{ submitFailed: true }}
  onChange={() => {
    const el = document.querySelector('.error')
    if (el) {
        el.scrollIntoView({ behavior: 'smooth' })
    }
  }}
/>

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

czterystaczwarty picture czterystaczwarty  路  4Comments

gsantiago picture gsantiago  路  5Comments

vlopp picture vlopp  路  4Comments

niros-welldone picture niros-welldone  路  3Comments

CodeWithOz picture CodeWithOz  路  4Comments