react-redux-form icon indicating copy to clipboard operation
react-redux-form copied to clipboard

Focus first invalid field onSubmitFailed

Open lydia-schow opened this issue 7 years ago • 2 comments

Goal: I would like to imitate native HTML5 behavior. When the user tries to submit an invalid form, the browser will focus on the first invalid field. This is really important for a good user experience on forms that require scrolling because the invalid field might be out of sight by the time the form is submitted.

Question: I'm having trouble figuring out how to do this. I think it might involve onSubmitFailed and actions.focus, but I don't know how to get the model name of the first invalid field. Any help?

I couldn't find any similar GitHub issues for this repository.

lydia-schow avatar Jun 25 '18 20:06 lydia-schow

The callback and action you mention are correct as far as I have experienced. Here's an example onSubmitFailed function to get the first failing model and focus it::

  handleSubmitFailed(form) {
    Object.keys(form).some((key) => {
      let obj = form[key];
      if (obj.valid === false) {
        this.props.store.dispatch(actions.focus(obj.model));
        return true;
      }
    });
  }

craig-landry avatar Jun 27 '18 22:06 craig-landry

This is a nice solution, but unfortunately the order of checking is not from top to bottom. Any advice on how to check from top to bottom in form would be appreciated :)

gormv avatar Oct 16 '18 13:10 gormv