问题描述
我想使用 oneOf 但它不起作用。我想检查密码和密码-重复是否匹配。在 nodejs 中它有效,但在 react native 中它不起作用。除了密码-重复之外,所有错误都显示出来。为什么?
const registerSchema = yup.object().shape({
fullName: yup.string()
.required('Your name is required.')
.min(4)
.max(40),email: yup.string()
.required()
.min(6)
.max(255)
.email(),password: yup.string()
.required()
.min(7)
.max(255),passwordRepeat: yup.string()
.oneOf([yup.ref('password'),null],'Password does not match')
});
我所有的 TextInput 都有值,并且 onChangeText 和 onBlur 以及除密码重复外的所有字段都可以正常工作
解决方法
试试这个方法
Yup.object().shape({
password: Yup.string().required("Required"),passwordRepeat: Yup.string()
.required("Required")
.when('password',(password,schema) => {
return schema.test({
test: (passwordRepeat) => password === passwordRepeat,message: 'Password does not match',});
}),});