Yup 中的条件验证
2022-08-30 05:16:55
我有一个电子邮件字段,仅当选中复选框时才会显示(布尔值为 )。当表单提交时,我只在选中复选框时需要此字段(布尔值为 true)。true
这是我到目前为止尝试过的:
const validationSchema = yup.object().shape({
email: yup
.string()
.email()
.label('Email')
.when('showEmail', {
is: true,
then: yup.string().required('Must enter email address'),
}),
})
我已经尝试了其他几种变体,但是我收到了Formik和Yup的错误:
Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
at yupToFormErrors (formik.es6.js:6198)
at formik.es6.js:5933
at <anonymous>
yupToFormErrors @ formik.es6.js:6198
我也从Yup那里得到了验证错误。我做错了什么?