基于另一个字段值的必填字段-Formik,Yup [英] field required based on value of another field - formik, yup
本文介绍了基于另一个字段值的必填字段-Formik,Yup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个反应表格, 在有选择字段的地方, 假设该字段具有值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.')
})
})
这篇关于基于另一个字段值的必填字段-Formik,Yup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文