简体   繁体   English

在 onBlur 模式下使用 react-hook-form 启用/禁用提交按钮

[英]Enable/disable submit button with react-hook-form in onBlur mode

I use react-hook-form in onChange mode.我在 onChange 模式下使用 react-hook-form。 I enable/disable the submit button depending on the validation state.我根据验证 state 启用/禁用提交按钮。 It works just fine.它工作得很好。

Now I have to switch to onBlur mode.现在我必须切换到 onBlur 模式。 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这是一个示例: 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.更新解决方案: NearHuscarl 的回答有助于找到解决方案。 I have to stay at the onChange mode and handle the display of the error message myself.我必须留在 onChange 模式并自己处理错误消息的显示。 I only displaying the error if the field is in the touchedFields object of the formState.如果该字段位于 formState 的touchedFields object 中,我只会显示错误。

  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工作示例: https://codesandbox.io/s/react-hook-form-disable-submit-button-onblur-forked-v8m05?file=/src/index.js

That is exactly how onBlur mode works.这正是onBlur模式的工作原理。 From the docs :文档

Validation will trigger on the blur event.验证将在模糊事件上触发。

So when all of your fields become 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.但是,一旦您在当前聚焦的字段之外单击,该字段的blur事件就会触发,该事件会运行并通过验证检查,然后才会再次启用提交按钮。

The solution is to change it back to onChange mode to trigger validation every time the input value changes.解决的办法是把它改回onChange模式,每次输入值改变时触发验证。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM