我如何检查密码是否与 yup 匹配

问题描述

我想使用 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',});
    }),});

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...