javascript 使用 yup 和 formik 进行密码验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49502436/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
password validation with yup and formik
提问by Strahinja Ajvaz
how would one go about having password validation but at the same time having the errors be passed to different variables?
如何进行密码验证但同时将错误传递给不同的变量?
i.e
IE
password: Yup.string().required("Please provide a valid password"),
passwordMin: Yup.string().oneOf([Yup.ref('password'), null]).min(8, 'Error'),
passwordLC: Yup.string().oneOf([Yup.ref('password'), null]).matches(/[a-z]/, "Error" )
passwordUC: Yup.string().oneOf([Yup.ref('password'), null]).matches(/[A-Z]/, "Error" )
I cant get the binding of the password variables to bind with the password object
我无法获得密码变量的绑定以与密码对象绑定
回答by VBorisoff
Just to elaborate on efleurine's answer.
You do not need to store each validation on the same field - you can chain them to get a full validation.
只是为了详细说明efleurine的答案。
您不需要将每个验证存储在同一字段上 - 您可以将它们链接起来以获得完整的验证。
password: Yup.string()
.required('No password provided.')
.min(8, 'Password is too short - should be 8 chars minimum.')
.matches(/[a-zA-Z]/, 'Password can only contain Latin letters.')
Note how you still can specify individual messages for each failure.
Also, for the binding to work, make sure the form input you're binding to has an appropriate nameattribute - in this case, password.
请注意您仍然可以为每个失败指定单独的消息。
此外,为了使绑定起作用,请确保您绑定到的表单输入具有适当的name属性 - 在本例中为password.
回答by efleurine
I know you can chain validations together for the same element. If you are trying to do cross-validation then this article would help you. it about formik in react but I bet it would give you an idea how to solve your case.
我知道您可以将同一元素的验证链接在一起。如果您正在尝试进行交叉验证,那么本文将对您有所帮助。它是关于 formik 在 react 中的,但我敢打赌,它会让你知道如何解决你的案子。
https://itnext.io/simple-react-form-validation-with-formik-yup-and-or-spected-206ebe9e7dcc
https://itnext.io/simple-react-form-validation-with-formik-yup-and-or-spected-206ebe9e7dcc

