如何使用YUP验证()方法在formik表单中动态显示/隐藏输入字段?
我是初学者,并尝试使用yup验证在reactj中创建表单。我正在尝试使用具有名称的非常简单的表单,即LastName。而且,我尝试仅在
使用useformik挂钩
const formik = useFormik({
initialValues: {
firstName: "",
lastName: "",
// email: ""
},
validationSchema: Yup.object({
firstName : Yup.string().max(15,"Must be 15 chars or less").required("required"),
lastName : Yup.string().when("firstName", {
is:true,
then: Yup.string().required("req")
})
}),
onSubmit:(values) => {
console.log(values)
}
})
yup验证验证一个名称并在提交func上
validationSchema: Yup.object({
firstName : Yup.string().max(15,"Must be 15 chars or less").required("required"),
lastName : Yup.string().when("firstName", {
is:true,
then: Yup.string().required("req")
})
}),
onSubmit:(values) => {
console.log(values)
}
})
验证我的实际表单时,我的实际表单
<form onSubmit={formik.handleSubmit}>
<div className="input-container">
<input
id="firstName"
name="firstName"
type="text"
placeholder="firstName"
onChange={formik.handleChange}
value={formik.values.firstName}
/>
{formik.errors.firstName ? <p>{formik.errors.firstName}</p> : null}
</div>
<div className="input-container">
<input
id="lastName"
name="lastName"
type="text"
placeholder="lastName"
onChange={formik.handleChange}
value={formik.values.lastName}
/>
</div>
<button type="submit">Submit</button>
</form>
是否不确定是否应该hide/hide hide/hide lastname input字段以表单中的形式中,我已经在formik incorik inormik初始值中将其声明了目的。
I am a beginner and trying to create a form in reactjs using Yup validation. I am trying to experiment with a very simple form that has firstName, lastName. And I am trying to only show lastName field when firstName is validated
using the useFormik hook
const formik = useFormik({
initialValues: {
firstName: "",
lastName: "",
// email: ""
},
validationSchema: Yup.object({
firstName : Yup.string().max(15,"Must be 15 chars or less").required("required"),
lastName : Yup.string().when("firstName", {
is:true,
then: Yup.string().required("req")
})
}),
onSubmit:(values) => {
console.log(values)
}
})
Yup validation and on submit func
validationSchema: Yup.object({
firstName : Yup.string().max(15,"Must be 15 chars or less").required("required"),
lastName : Yup.string().when("firstName", {
is:true,
then: Yup.string().required("req")
})
}),
onSubmit:(values) => {
console.log(values)
}
})
My actual form
<form onSubmit={formik.handleSubmit}>
<div className="input-container">
<input
id="firstName"
name="firstName"
type="text"
placeholder="firstName"
onChange={formik.handleChange}
value={formik.values.firstName}
/>
{formik.errors.firstName ? <p>{formik.errors.firstName}</p> : null}
</div>
<div className="input-container">
<input
id="lastName"
name="lastName"
type="text"
placeholder="lastName"
onChange={formik.handleChange}
value={formik.values.lastName}
/>
</div>
<button type="submit">Submit</button>
</form>
Not sure if should hide/show the lastName input field in the form, I have already declared it inside Formik initial values object.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两件事要处理:
lastName
当firstName
具有值时。如果该假设是正确的,则可以执行这样的操作:在上面的示例中,
is :( firstName)=&gt; !! firstName
正在检查firstName
是否具有值。如果仅在firstName
为有效时才需要lastName
,则可以将该行更改为is :( firstNname)=&gt; firstName&amp; amp; firstName.length&lt; = 15
,您也可以将其缩短一点,但是上述解决方案应起作用。缩短版本:
There are two things to deal with here:
lastName
whenfirstName
has a value. If that assumption is correct, you can do something like this:In the above example,
is: (firstName) => !!firstName
is checking iffirstName
has a value. If you wanted to requirelastName
only whenfirstName
is valid, you could change that line tois: (firstName) => firstName && firstName.length <= 15
You could also shorten it a bit but the above solution should work. Shortened version: