在React Hook Form中,当必填字段为空时禁用按钮按下。

huangapple go评论83阅读模式
英文:

Disable button presses when required fields are empty in react-hook-form

问题

要实现在以下代码中只有在"Name"和"State"都为空时禁用按钮,您可以使用以下方法。换句话说,只有在输入了"name"和"state"时,无论"email"是否为空,按钮才能被点击。

import React from "react";
import { useForm, SubmitHandler } from "react-hook-form";

type FormValues = {
  name: string;
  state: string;
  email: string;
};

export default function App() {
  const { register, handleSubmit, watch, formState } = useForm<FormValues>();
  const { isDirty, isValid } = formState;
  const onSubmit: SubmitHandler<FormValues> = data => console.log(data);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("name")} />
      <input {...register("state")} />
      <input type="email" {...register("email")} />

      <input type="submit" disabled={!isDirty || !isValid} />
    </form>
  );
}

这里,我们使用isDirty来检查表单是否被修改过,以及isValid来检查表单是否有效。只有在"Name"和"State"都不为空且表单被修改过时,按钮才会可用。

英文:

I would like to know how to disable a button only when both Name and State in the following code are empty.In other words, I would like to know how to make it so that the button can be pressed only when both name and state are entered, regardless of whether the email is null or not.

import React from &quot;react&quot;;
import { useForm, SubmitHandler } from &quot;react-hook-form&quot;;

type FormValues = {
  name: string;
  state: string;
  email: string;
};

export default function App() {
  const { register, handleSubmit } = useForm&lt;FormValues&gt;();
  const onSubmit: SubmitHandler&lt;FormValues&gt; = data =&gt; console.log(data);

  return (
    &lt;form onSubmit={handleSubmit(onSubmit)}&gt;
      &lt;input {...register(&quot;name&quot;)} /&gt;
      &lt;input {...register(&quot;state&quot;)} /&gt;
      &lt;input type=&quot;email&quot; {...register(&quot;email&quot;)} /&gt;

      &lt;input type=&quot;submit&quot; /&gt;
    &lt;/form&gt;
  );
}

答案1

得分: 1

你可以通过检查formState对象的isValid属性来实现这一点。当isValid为false时禁用按钮,并在register方法中为namestate设置required属性为true。这样,只要namestate为空,按钮就会被禁用,不管电子邮件输入是什么。

以下是一个可工作的示例:

import React from "react";
import { useForm, SubmitHandler } from "react-hook-form";

type FormValues = {
  name: string;
  state: string;
  email: string;
};

export default function App() {
  const { register, handleSubmit, formState } = useForm<FormValues>();
  const onSubmit: SubmitHandler<FormValues> = data => console.log(data);

  return (
    <form onSubmit={handleSubmit(onSubmit)}>
      <input {...register("name", {required: true})} />
      <input {...register("state", {required: true})} />
      <input type="email"   {...register("email")}/>

      {/* 如果Name和State都为空,则禁用输入按钮 */}
      <input type="submit" disabled={!formState.isValid} />
    </form>
  );
}
英文:

You can achieve that with checking the property isValid of the formState object. Disable the button when isValid is false and set the required property within the register method to true for name and state. The button is disabled this way whenever name or state are empty, regardless of the email input.

Here is a working version:

import React from &quot;react&quot;;
import { useForm, SubmitHandler } from &quot;react-hook-form&quot;;

type FormValues = {
  name: string;
  state: string;
  email: string;
};

export default function App() {
  const { register, handleSubmit, formState } = useForm&lt;FormValues&gt;();
  const onSubmit: SubmitHandler&lt;FormValues&gt; = data =&gt; console.log(data);

  return (
    &lt;form onSubmit={handleSubmit(onSubmit)}&gt;
      &lt;input {...register(&quot;name&quot;, {required: true})} /&gt;
      &lt;input {...register(&quot;state&quot;, {required: true})} /&gt;
      &lt;input type=&quot;email&quot;   {...register(&quot;email&quot;)}/&gt;

      {/* Disable input button if both Name and State are empty */}
      &lt;input type=&quot;submit&quot; disabled={!formState.isValid} /&gt;
    &lt;/form&gt;
  );
}

huangapple
  • 本文由 发表于 2023年2月27日 01:35:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573838.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定