【问题标题】:How can I access the value of input inside yup object?如何访问 yup 对象内的输入值?
【发布时间】:2021-06-01 05:20:04
【问题描述】:

我在 react 中使用 formik 进行验证,但现在我需要 yup.object({}) 中的 firstName 值。我怎样才能访问它?

const validationSchema = yup.object({
        firstName: yup
          .string("Enter your First Name")
          .required("First Name is required"),
    })
 const formik = useFormik({
    initialValues: {
      firstName: ""
    },
    validationSchema: validationSchema,
})

【问题讨论】:

    标签: reactjs material-ui formik yup


    【解决方案1】:

    您可以为此使用test

    const validationSchema = yup.object({
            firstName: yup
              .string("Enter your First Name")
              .test(name for the test, validation message, (value) => this gives your value)
        })
    

    参考

    1.Using Test in Yup

    2.Using When in Yup

    【讨论】:

      猜你喜欢
      • 2020-05-14
      • 1970-01-01
      • 2011-11-04
      • 2020-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多