是否可以使用YUP和React-Hook-Forms使用两个不同形式的不同模式?
我有一个基本表单,有两个输入input1
和input2
。在第一个架构中,两个字段都是必需的,但是在第二个架构中,只需要第一个字段。我在同一形式上有两个按钮。按下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论