javascript - Enabledisable submit button with react-hook-form in onBlur mode - Stack Overflow

I use react-hook-form in onChange mode. I enabledisable the submit button depending on the validation

I use react-hook-form in onChange mode. I enable/disable the submit button depending on the validation state. It works just fine.

Now I have to switch to onBlur mode. And the current solution is not working as expected anymore. When all the field became valid I expect the submit button became enabled immediately. Now it only became enabled when I click out of the last field. What should I change in my code to work as expected?

  const { register, errors, handleSubmit, formState } = useForm({
    mode: "onChange" // I want to change it to onBlur
  });
  const { isValid } = formState;

...

<input disabled={!isValid} type="submit" />

Here is an example: =/src/index.js

UPDATE WITH SOLUTION: NearHuscarl's answer helped to find the solution. I have to stay at the onChange mode and handle the display of the error message myself. I only displaying the error if the field is in the touchedFields object of the formState.

  const form = useForm({
    mode: "onChange" // I want to change it to onBlur
  });
  const { register, handleSubmit, formState } = form;
  const { isValid, touchedFields, errors } = formState;

...

        <div>
          <label htmlFor="firstName">First Name</label>
          <input
            name="firstName"
            placeholder="bill"
            {...register("firstName", { minLength: 3 })}
          />
          {errors.firstName && touchedFields.firstName && (
            <p>"minimum length is 3"</p>
          )}
        </div>

Working example: =/src/index.js

I use react-hook-form in onChange mode. I enable/disable the submit button depending on the validation state. It works just fine.

Now I have to switch to onBlur mode. And the current solution is not working as expected anymore. When all the field became valid I expect the submit button became enabled immediately. Now it only became enabled when I click out of the last field. What should I change in my code to work as expected?

  const { register, errors, handleSubmit, formState } = useForm({
    mode: "onChange" // I want to change it to onBlur
  });
  const { isValid } = formState;

...

<input disabled={!isValid} type="submit" />

Here is an example: https://codesandbox.io/s/react-hook-form-disable-submit-button-onblur-0293x?file=/src/index.js

UPDATE WITH SOLUTION: NearHuscarl's answer helped to find the solution. I have to stay at the onChange mode and handle the display of the error message myself. I only displaying the error if the field is in the touchedFields object of the formState.

  const form = useForm({
    mode: "onChange" // I want to change it to onBlur
  });
  const { register, handleSubmit, formState } = form;
  const { isValid, touchedFields, errors } = formState;

...

        <div>
          <label htmlFor="firstName">First Name</label>
          <input
            name="firstName"
            placeholder="bill"
            {...register("firstName", { minLength: 3 })}
          />
          {errors.firstName && touchedFields.firstName && (
            <p>"minimum length is 3"</p>
          )}
        </div>

Working example: https://codesandbox.io/s/react-hook-form-disable-submit-button-onblur-forked-v8m05?file=/src/index.js

Share Improve this question edited Apr 8, 2021 at 12:40 Peter Ambruzs asked Apr 7, 2021 at 8:13 Peter AmbruzsPeter Ambruzs 8,2234 gold badges33 silver badges38 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

That is exactly how onBlur mode works. From the docs:

Validation will trigger on the blur event.

So when all of your fields bee valid, it does not trigger validation. But once you click outside of the currently focused field, the blur event of that field fires, which runs and passes the validation check, only then the submit button is enabled again.

The solution is to change it back to onChange mode to trigger validation every time the input value changes.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744224977a4563961.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信