我创建了一个注册表单,但在尝试验证密码确认时出现了问题。我正在使用最新版本的 JOI-Browser。
我尝试了下面的代码,即使密码和密码确认具有相同的值,也会触发验证错误。
password: Joi.string()
.min(5)
.required(),
passwordConfirmation: Joi.ref("password")
这是我的状态对象:
password: "12345"
passwordConfirmation: "12345"
username: ""
errors: {…}
passwordConfirmation: "\"passwordConfirmation\" must be one of [ref:password]"
我花了几个小时尝试了几种方法并阅读了文档,但仍然没有运气,验证仍在触发,
我在此表单中进行了其他验证,它们工作正常。
最佳答案
我认为 Joi.ref
不应该这样使用。
我通常倾向于这样做:
const passwordConfirmation = Joi.string()
.required()
.valid(Joi.ref('password'))
.options({
language: {
any: {
allowOnly: '!!Passwords do not match',
}
}
})
如果您引用docs ,你会看到:
Note that references can only be used where explicitly supported such as in valid() or invalid() rules. If upwards (parents) references are needed, use object.assert().
关于javascript - 密码确认和密码相同,但验证仍然触发 React 组件中的 JOI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56779021/