如何使用动态字段名称创建 Formik YUP 架构?
How do you Create a Formik YUP Schema with Dynamic Field Names?
如何使用动态点表示法字段名称创建 YUP 架构?以下架构无效。我将如何遍历 Costs.0.item、Costs.1.item、Costs.2.item AND Costs.0.amount、Costs.1.amount、Costs.2.amount动态?感谢您的帮助!
const IpSchema = Yup.object().shape({
Project_Title: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.0.item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.1.item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.2.item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.0.amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.1.amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.2.amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
});
在这种情况下您必须使用 yup.array
,即:
const IpSchema = Yup.object().shape({
Project_Title: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs: Yup.array().of(
Yup.object({
item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required')
amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required')
})
),
});
如果您将 YUP
与 formik
一起使用,您可以检查 this. On docs provided example. And otherwhise according docs 您可以这样做:
const IpSchema = Yup.object().shape({
Project_Title: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs: Yup.array().of(
Yup.object()
.shape({
item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required')
})
),
});
如何使用动态点表示法字段名称创建 YUP 架构?以下架构无效。我将如何遍历 Costs.0.item、Costs.1.item、Costs.2.item AND Costs.0.amount、Costs.1.amount、Costs.2.amount动态?感谢您的帮助!
const IpSchema = Yup.object().shape({
Project_Title: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.0.item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.1.item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.2.item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.0.amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.1.amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs.2.amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
});
在这种情况下您必须使用 yup.array
,即:
const IpSchema = Yup.object().shape({
Project_Title: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs: Yup.array().of(
Yup.object({
item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required')
amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required')
})
),
});
如果您将 YUP
与 formik
一起使用,您可以检查 this. On docs provided example. And otherwhise according docs 您可以这样做:
const IpSchema = Yup.object().shape({
Project_Title: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
Costs: Yup.array().of(
Yup.object()
.shape({
item: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required'),
amount: Yup.string()
.min(2, 'Too Short!')
.max(255, 'Too Long!')
.required('Required')
})
),
});