Javascript 使用Yup访问必填字段
我使用react hook表单和yup来验证我的表单 我想知道模式的所有必填字段,以便在表单中显示一些信息(如必填字段的“*”)。 我们可以通过以下代码实现这一点:Javascript 使用Yup访问必填字段,javascript,reactjs,yup,react-hook-form,Javascript,Reactjs,Yup,React Hook Form,我使用react hook表单和yup来验证我的表单 我想知道模式的所有必填字段,以便在表单中显示一些信息(如必填字段的“*”)。 我们可以通过以下代码实现这一点: schema.descripe().fields[field].tests.findIndex(({name})=>name=='required' 但是,此代码不适用于条件验证 模式示例: const schema = yup.object().shape({ email: yup .string() .e
schema.descripe().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()
})
})
有没有办法在表单中检索这些信息?实际上没有“好”的方法,但这很有效:
function isRequired(field){
return schema.fields[field]._exclusive.required || false
}
注意:schema.fields[field]。_exclusive.required
如果需要或未定义,则返回true