作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 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()
})
})
有没有办法在表单中检索这些信息?
最佳答案
实际上没有“好”的方法可以做到,但这很有效:
function isRequired(field){
return schema.fields[field]._exclusive.required || false
}
注意:schema.fields[field]._exclusive.required
如果需要或未定义,则返回 true。
关于javascript - 使用 Yup 访问必填字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64440400/
我是一名优秀的程序员,十分优秀!