Javascript 使用Formik&;在FieldArray中进行条件验证;是的
我有一个具有嵌套值的ScheduleForm,名为Javascript 使用Formik&;在FieldArray中进行条件验证;是的,javascript,reactjs,formik,yup,Javascript,Reactjs,Formik,Yup,我有一个具有嵌套值的ScheduleForm,名为hours\u attributes。单个小时有一个绑定条件,如果全天值为假,则需要出现打开值。以下是我当前架构的编写方式: const ScheduleSchema = Yup.object().shape({ name: Yup.string() .required('Required'), hours_attributes: Yup.array().of( Yup.object().shape({ ope
hours\u attributes
。单个小时有一个绑定条件,如果全天
值为假,则需要出现打开
值。以下是我当前架构的编写方式:
const ScheduleSchema = Yup.object().shape({
name: Yup.string()
.required('Required'),
hours_attributes: Yup.array().of(
Yup.object().shape({
opens: Yup.object().when('all_day', {
is: false,
then: Yup.string().required('Required'),
otherwise: Yup.string()
}),
}))
});
我不确定
when
值是否期望该特定小时的全天值的索引。我需要某种索引吗?尝试在以下情况下为的第二个参数提供函数:
const ScheduleSchema = Yup.object().shape({
name: Yup.string()
.required('Required'),
hours_attributes: Yup.array().of(
Yup.object().shape({
opens: Yup.boolean().when('all_day', (value, schema) =>
value ? schema : schema.required('Required')),
}))
});
如果上述代码段不起作用,则更改hours\u属性,如:
Yup.object({
opens: Yup.boolean().when('all_day', (value, schema) =>
value ? schema : schema.required('Required'))
}),
如果希望打开的值为真,则:
Yup.object({
opens: Yup.boolean().when('all_day', (value, schema) =>
value ? schema : schema.oneOf([true], 'Required'))
}),
打开的类型是什么?它是一个布尔值。这是否意味着当全天
为false时,打开
应为true?如果是,那么您应该像这样尝试:js Yup.object({opens:Yup.boolean().when('all_day',(value,schema)=>value?schema:schema.oneOf([true],'opens should is true')),