问题描述
我有一个反应表格, 在有选择字段的地方, 假设该字段具有值A、B、C、D、E、F。
现在假设,另一个字段ChooseSubType只有在我选择B或D时才会显示,并且此字段仅在显示时是必填字段,而不是在此之前。
现在,我如何才能做到这一点?
以下是第一个字段的代码,即选择字段
chooseAlphabet: Yup.string().required('field required'),
chooseSubType : Yup.string().when('chooseAlphabet',
('chooseAlphabet',schema)=>{
console.log('value business : ',chooseAlphabet);
if(chooseAlphabet === "B"||"D"){
return schema;
}else{
return schema.required('field required');
}
}),
但此代码不起作用。
现在,我要在此处进行哪些更改才能使它按我希望的方式工作?
推荐答案
我知道有点晚了,但您可以试试
chooseSubType: yup.string().when('chooseAlphabet', {
is: 'B',
then: schema => schema,
otherwise: yup.string().when('type', {
is: 'D',
then: yup.string().required('field required.')
})
})