Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue: When using reValidateMode: 'onBlur', the form doesn't submit when its single error is fixed #11866

Closed
1 task done
stowball opened this issue May 6, 2024 · 6 comments
Labels
question Further information is requested

Comments

@stowball
Copy link

stowball commented May 6, 2024

Version Number

7.51.4

Codesandbox/Expo snack

https://codesandbox.io/p/sandbox/react-hook-form-handlesubmit-v7-forked-wkshgg?file=%2Fsrc%2Findex.jsx

Steps to reproduce

When using reValidateMode: 'onBlur' and your form has 1 error, if your blur event is also clicking the button to submit the form, then the errors are removed, but the submit never happens.

Screencast of below steps:

react-hook-form.mp4

Steps to reproduce:

  1. Go to https://codesandbox.io/p/sandbox/react-hook-form-handlesubmit-v7-forked-wkshgg?file=%2Fsrc%2Findex.jsx
  2. Click on the Submit button with your mouse
  3. Type "a" into focused form field
  4. Click on the Submit button with your mouse without performing any other blur action

Expected behaviour

When the form has no errors, it should submit successfully when clicking the submit button

What browsers are you seeing the problem on?

Firefox, Chrome, Safari, Edge

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@bluebill1049
Copy link
Member

The input would need to fire an onBlur event for the revalidate.

@bluebill1049 bluebill1049 added the question Further information is requested label May 7, 2024
@stowball
Copy link
Author

stowball commented May 7, 2024

But how? It feels very wrong to need to intercept onClick on the submit button, blur, refocus and then submit… if that's even a possible solution to the problem

@bluebill1049
Copy link
Member

maybe try to do a trigger on the submit button click?

@stowball
Copy link
Author

stowball commented May 9, 2024

That doesn't seem to work either. The onClick doesn't seem to fire when there are errors:

const handleClick = () => {
    console.log(Math.radom());
    trigger();
    handleSubmit(onSubmit)();
  };

  return (
    <div className="App">
      <form onSubmit={handleSubmit(onSubmit)}>
        {errors?.firstName ? <p>Enter a valid name</p> : null}
        <div>
          <label htmlFor="firstName">First Name</label>
          <input
            {...register("firstName", {
              required: true,
            })}
          />
        </div>
        <input type="submit" onClick={handleClick} />
      </form>
    </div>
  );

The random number gets logged on the first press which initially validates the form, but not on the 2nd.

Was that what you had in mind?

@bluebill1049
Copy link
Member

yea that's what i have in mind, why it's not firing tho. it should as the onClick even there.

@erashu212
Copy link
Contributor

Hi @bluebill1049,

If I understood correctly, you want the form to be submitted onBlur if form is valid. This is not expected behavior of reValidationMode. However you can achieve this by using below code snippet.

import React from "react";
import ReactDOM from "react-dom";
import { useForm } from "react-hook-form";

import "./index.css";

function App() {
  const { register, handleSubmit, formState } = useForm({
    mode: "onSubmit",
    reValidateMode: "onBlur",
  });
  const [blurState, setBlurState] = React.useState({})

  const onSubmit = (data) => {
    alert(JSON.stringify(data));
  };

  const { onBlur, ...registerProps } = register('firstName', { required: true }); 

  const handBlurEvent = (e) => {
    onBlur(e)
    setBlurState(prevState => ({
      ...prevState,
      "firstName": true
    }))
  };

  const isValidState = blurState["firstName"] && !formState.isValidating && !formState.errors["firstName"]

  React.useEffect(() => {
    if(isValidState) {
      handleSubmit(onSubmit)()
      setBlurState(prevState => ({
        ...prevState,
        "firstName": false
      }))
    }
  }, [
    isValidState,
    onSubmit,
    handleSubmit
  ])

  return (
    <div className="App">
      <form onSubmit={handleSubmit(onSubmit)}>
        {formState.errors && formState.errors.firstName ? (
          <p>Enter a valid name</p>
        ) : null}
        <div>
          <label htmlFor="firstName">First Name</label>
          <input
            {...registerProps}
            onBlur={handBlurEvent}
          />
        </div>
        <input type="submit" />
      </form>
    </div>
  );
}

const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);

@react-hook-form react-hook-form locked and limited conversation to collaborators May 19, 2024
@bluebill1049 bluebill1049 converted this issue into discussion #11900 May 19, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants