我有一个形式来创建一个课程与作者。与一位作者填写表格 x- 格式与两位作者
如何为作者数组创建Yup模式?它应该是作者的数组(至少一个作者,大写名字和姓氏)。
这是我的基本模式,当它是一个输入时:
const COURSE_SCHEMA = yup.object().shape({
title: yup
.string()
.min(2, 'Too Short!')
.max(16, 'Too Long!')
.required('Required'),
description: yup
.string()
.min(24, 'Too Short!')
.max(124, 'Too Long!')
.required('Required'),
authors: yup.string().required('Required'),
duration: yup
.number('Paste the duartion time in minutes')
.integer('The number has to be integer')
.min(15, 'At least 15 minutes!')
.max(300, 'Maximum duration is 300 minutes!')
.required('Required'),
});
const yupSchemas = {
// loginSchema: LOGIN_SCHEMA,
// registrationSchema: REGISTERATION_SCHEMA,
courseSchema: COURSE_SCHEMA,
};Formik init:
<Formik
validationSchema={yupSchemas.courseSchema}
initialValues={{
title: '',
description: '',
authors: [''],
duration: '',
}}
//-----------------这是我的动态多值输入。它是由这个福米克模板制作的
<FormStrap.Group className='mb-3' controlId='course-authors'>
<FormStrap.Label>Authors</FormStrap.Label>
<FieldArray
name='authors'
render={(arrayHelpers) => (
<Box>
{values.authors && values.authors.length > 0 ? (
values.authors.map((authors, index) => (
<Box key={index}>
<Field
className='form-control'
name={`authors.${index}`}
type='text'
placeholder='Name Surname'
/>
<Button
className='mt-1'
variant='success'
size='sm'
onClick={() => arrayHelpers.insert(index, '')}
>
+
</Button>{' '}
<Button
className='mt-1'
variant='danger'
size='sm'
onClick={() => arrayHelpers.remove(index)}
disabled={values.authors.length === 1}
>
-
</Button>
</Box>
))
) : (
<Button onClick={() => arrayHelpers.push('')}>
Add an Author
</Button>
)}
</Box>
)}
/>
</FormStrap.Group>发布于 2022-08-29 22:29:21
这个答案是正确的。感谢乌萨马。
authors: yup.array().of(yup.string())https://stackoverflow.com/questions/72580649
复制相似问题