我想使用 formik 进行异步验证,并使用 yup 进行validationschema,但我找不到示例或演示。
最佳答案
const validationSchema = Yup.object().shape({
username:
Yup.string()
.test('checkDuplUsername', 'same name exists', function (value) {
return new Promise((resolve, reject) => {
kn.http({
url: `/v1/users/${value}`,
method: 'head',
}).then(() => {
// exists
resolve(false)
}).catch(() => {
// note exists
resolve(true)
})
})
})
})
是的,通过测试方法提供异步处理。
(kn是我的ajax promise 函数)
祝你有美好的一天。
关于reactjs - 使用 Formik、Yup 和 React 进行异步验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55811114/