删除输入字段上所需的验证(如果我在另一个输入字段中输入了一些文本)

时间:2020-02-08 02:52:35

标签: javascript reactjs react-hooks react-hook-form

删除输入字段上需要的验证(如果我在另一个输入字段中输入了一些文本)。我从json创建了输入字段(两个输入字段)。最初都需要。但是如果用户在第一个字段中输入“ hello”从第二个输入字段中删除所需的检查。我使用了watch和onchange,我检查了第一个字段的值。但是如何删除所需的检查?我尝试使用unregister但仍然无法工作

const onChange = e => {
    console.log(e.target.name);
    if (e.target.name == "agencyName") {
      if (agencyName == "hello") {
        //remove required
        unregister({ required: false, name: "contactPerson" });
      }
    }
  };

这是我的代码 https://codesandbox.io/s/react-hook-form-watch-unir2

API链接

https://react-hook-form.com/api/

1 个答案:

答案 0 :(得分:3)

这是更新后的脚本,当代理商名称等于“ hello”时,该联系人将成为可选联系人。

https://codesandbox.io/s/react-hook-form-watch-j396u

相关问题