我想要favoriteAlcohol
仅当以下情况时才需要验证/需要的字段props.isAdult
传递给组件的是true
.
Yup.object().shape({
favoriteAlcohol: Yup.string().when(props.isAdult, {
is: true,
then: Yup.string().required(),
}),
name: Yup.string().required('Nazwa jest wymagana'),
})
我怎样才能做到这一点?
上述解决方案不起作用,因为when
将表单的“键”作为第一个参数,并且我传递了 prop.
一种可行的解决方案是绘制地图prop.isAdult
to isAdult
表单值字段,然后我可以通过'isAdult'
作为第一个参数when()
功能。但这不是最好的解决方案。
const favoriteAlcohol = props.isAdult ? {favoriteAlcohol: Yup.string().required()} : {}
Yup.object().shape({
...favoriteAlcohol,
name: Yup.string().required('Nazwa jest wymagana'),
})
如果 props.isAdult = true 那么,它会让
Yup.object().shape({
favoriteAlcohol: Yup.string().required(),
name: Yup.string().required('Nazwa jest wymagana'),
})
否则,就会使
Yup.object().shape({
name: Yup.string().required('Nazwa jest wymagana'),
})
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)