Set focus in first faulty field
See original GitHub issueHi,
is there a way to always set the focus to the first faulty field?
I tried to implement a solution in my render field component:
componentDidUpdate() {
const { meta: { submitting, touched, error } } = this.props;
const field = this.refs['field' + this.props.name];
...
if (!submitting && touched && error) {
field.select();
}
}
This works partially, but…
If I enter one char in the first faulty field, the validation is at this moment valid for the field and the next faulty field steals my focus.
How can I prevent this or is there already a built-in solution?
Thanks for your help!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:1
- Comments:16 (5 by maintainers)
Top Results From Across the Web
Bring Focus to the First Form Field with an Error
For simplicity, and based on personal preference, I am going to start with setting up my form to validate when the form is...
Read more >How to focus on first input of error in js where return false is ...
Option 1 - Add class to the fields with error and then you can use below code to focus the first error element....
Read more >How to set the focus to the first form field ? - GeeksforGeeks
Approach 1: Using HTML <input> autofocus attribute. Autofocus is a boolean attribute, used along with an <input> element in a form. The form ......
Read more >how to set the focus to the first input-field of a form?
In my case i have an ajaxed submit that refreshes only a certain zone but i have to set focus after the submit....
Read more >How to set focus to first error field. - SAP Community
My users want the cursor positioned to the first error field on the page after validation errors occur. WDA does a nice job...
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 FreeTop 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
Top GitHub Comments
All suggestions (in all corresponding/found issues) for implementing focusing the first field error were failing for me… then I worked out why… when using
redux-form/immutable
theonSubmitFail
errors
object does not contain the fields in registration order (possibly requires an ordered list reviver when setting/getting fields?).Also most do numerous DOM queries (query per field). So I solved it with one single DOM query:
In
onSubmitFail
Get all the error’ed fields and query them usingquerySelector
. This is the most workable/optimal asquerySelector
“Returns the first Element within the document that matches the specified selector, or group of selectors.” … so even if the selector is not in field order… it still returns the first error’ed field in the document, then justfocus()
it.Could possibly also
scrollIntoView()
though in my case this failed as the site has a fixed/floating header.Example
Notes
flattenObject
actually creates correct field names#1336 #488
I just recently started using
redux-forms
. I am working on an existing project that is using version 6.8.0. (Based on the implementation and the v7 docs, I think it will work in v7 too.)This solution uses 2 utility functions and the
onSubmitFail
callback.There are two main steps that need to be done for each form:
name
attribute to theform
element (just reuse the unique one you give toreduxForm
)onSubmitFail
callback as illustrated above.This solution solves all the issues I believe have been plaguing everyone.
redux-form
.Notes:
redux-forms
(but I am pretty sure it should work).focusFirstInvalidControl
can be used in any HTML document regardless of it being generated by React.Please let me know if these utilities already exist in an npm package somewhere. If not and you have a cleaner implementation (maybe one that doesn’t use recursion), then please let me know.
Edit: I whipped up an npm package that will set focus on the first invalid control. See qc-dom_utils. You can use the following in place of the
FormUtils.js
module above.Edit: I searched in npm and I searched the redux-form code base, but I didn’t find anything to flatten the errors. So, I created the qc-redux-form_utils package.