简体   繁体   中英

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 ?

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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