小编Spo*_*y30的帖子

使用 Yup 访问必填字段

我使用react-hook-form 和 yup 来验证我的表单。

我想知道模式的所有必填字段以在表单中显示一些信息(例如必填字段的“*”)。我们可以用这行代码来实现这一点:

schema.describe().fields[field].tests.findIndex(({ name }) => name === 'required'

但是,此代码不适用于条件验证。

架构示例:

const schema = yup.object().shape({
    email: yup
    .string()
    .email()
    .required(),

    isProfileRequired: yup
    .boolean(),

    profile:  yup
    .object()
    .when('isProfileRequired',{
        is: (isProfileRequired) => isProfileRequired,
        then:
            yup
            .object()
            .nullable()
            .required()
    })
})
Run Code Online (Sandbox Code Playgroud)

有没有办法在表单中检索这些信息?

javascript reactjs yup react-hook-form

6
推荐指数
2
解决办法
7272
查看次数

标签 统计

javascript ×1

react-hook-form ×1

reactjs ×1

yup ×1