简体   繁体   English

如何解决多个字段名称未知的问题。 使用 yup 和 reactjs

[英]How to resolve issue with mutiple field name unknown . Use yup and reactjs

I have array field name ... unknown field name , array field name get from database.我有数组字段名称...未知字段名称,数组字段名称从数据库中获取。 They is same validation .How to validation with yup unknown field name ?它们是相同的验证。如何使用 yup 未知字段名称进行验证?

const schema=yup.object().schema({
nameField1:yup.required('required')
        .min(5, 'min 5 characters')
        .max(10, 'max 10 characters'),
nameField2:yup.required('required')
        .min(5, 'min 5 characters')
        .max(10, 'max 10 characters'),
....
nameUnknown:yup.required('required')
        .min(5, 'min 5 characters')
        .max(10, 'max 10 characters'),
})

I had a similar problem when working with dynamic forms.使用动态表单时我遇到了类似的问题。 Meaning the backend would send me an object which I would use as a 'blueprint' if you will to make the form.这意味着后端会向我发送一个对象,如果您愿意制作表单,我会将其用作“蓝图”。 In that case what worked for me was to use the form library validation method (in formik it's validateField ) I think.在那种情况下,我认为对我有用的是使用表单库验证方法(在 formik 中它是validateField )。 Now, I'm not that familiar with react-hook-form , but I think doing your validations in the template like here would solve your problem.现在,我对react-hook-form不太熟悉,但我认为像这里这样在模板中进行验证可以解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM