是否可以使用YUP和React-Hook-Forms使用两个不同形式的不同模式?

发布于 2025-02-08 21:28:56 字数 892 浏览 3 评论 0原文

我有一个基本表单,有两个输入input1input2。在第一个架构中,两个字段都是必需的,但是在第二个架构中,只需要第一个字段。我在同一形式上有两个按钮。按下button1按下button2时,是否可以触发首次验证模式?如果是,那么最好的方法是什么?

const schema1 = yup.object().shape({
              input1: yup.string().email().required(),
              input2: yup.string().min(4).max(20).required(),
              });
const schema2 = yup.object().shape({
              input1: yup.string().email().required(),
              input2: yup.string().min(4).max(20),
              });

表格与以下方式相似:

const {handleSubmit} = useFormContext();

<form onSubmit= {handleSubmit(onSubmit)}>
       <input name="input1" /> 
       <input name="input2" />  

       <button type="submit">button1</button>
       <button type="submit">button2</button>
</form>

I have a basic form that has two inputs input1 and input2. In the first schema both fields are required but in the second schema, only the first field is required.I have two buttons on the same form. Is it possible to trigger first validation schema when pressing button1 an second on pressing button2? If yes, what is the best way to do it?

const schema1 = yup.object().shape({
              input1: yup.string().email().required(),
              input2: yup.string().min(4).max(20).required(),
              });
const schema2 = yup.object().shape({
              input1: yup.string().email().required(),
              input2: yup.string().min(4).max(20),
              });

The form is similar to this:

const {handleSubmit} = useFormContext();

<form onSubmit= {handleSubmit(onSubmit)}>
       <input name="input1" /> 
       <input name="input2" />  

       <button type="submit">button1</button>
       <button type="submit">button2</button>
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文