//Basicaly all I want to do is say is if region = "Europe" or "other" then the field is required
   , did go through the formik documentation but not finding anything.
   Am also new to formik so dont know if this is a nube question or not

<Formik
                enableReinitialize={true}
                initialValues={{
                    name: currentBankData.name || '',
                    address: currentBankData.address || '',
                    country: currentBankData.country || '',
                    region: currentBankData.region || '',
                    city: currentBankData.city || '',
                    swiftCode: currentBankData.swiftCode || '',
                    routeCode: currentBankData.routeCode || '',
                }}
                validationSchema={Yup.object().shape({
                    name: Yup.string().min(3).required('Name is required.'),
                    address: Yup.string().required('Address is required.'),
                    country: Yup.string().required('Country is required.'),
                    region: Yup.string().required('Region is required.'),
                    city: Yup.string().required('City is required.'),

                    swiftCode: Yup.string().when('region', {
                        is: 'Europe' || 'other,      //Would like to do something like this but 
                                                     doesnt work :)

                        then: Yup.string()
                            .required('SwiftCode is required.')
                            .matches(
                                /[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
                                'This is not the correct Swift Code'
                            ),
                    }),

                    routeCode: Yup.string().when('region', {
                        is: 'USA',
                        then: Yup.string().required('RouteCode is required.'),
                    }),

                    verified: Yup.bool(),
                })}

/ इस डेटा की कोई कविता नहीं / ऐसा लगता है कि आपकी पोस्ट ज्यादातर कोड है; कृपया कुछ और विवरण जोड़ें। ऐसा लगता है कि आपकी पोस्ट ज्यादातर कोड है; कृपया कुछ और विवरण जोड़ें। ऐसा लगता है कि आपकी पोस्ट ज्यादातर कोड है; कृपया कुछ और विवरण जोड़ें। ऐसा लगता है कि आपकी पोस्ट ज्यादातर कोड है; कृपया कुछ और विवरण जोड़ें। ऐसा लगता है कि आपकी पोस्ट ज्यादातर कोड है; कृपया कुछ और विवरण जोड़ें। ऐसा लगता है कि आपकी पोस्ट ज्यादातर कोड है; कृपया कुछ और विवरण जोड़ें।

0
ChrisJnr Potgieter 19 सितंबर 2020, 01:06

2 जवाब

सबसे बढ़िया उत्तर
    Finally Works
validationSchema={Yup.object().shape({
                    name: Yup.string().required('Name is required.'),
                    address: Yup.string().required('Address is required.'),
                    country: Yup.string().required('Country is required.'),
                    region: Yup.string().required('Region is required.'),
                    city: Yup.string().required('City is required.'),

                    swiftCode: Yup.string().when('region', (region, schema) => {
                        return ['Europe', 'other'].includes(region)
                            ? schema
                                    .required('SwiftCode is required.')
                                    .matches(
                                        /[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
                                        'This is not the correct Swift Code'
                                    )
                            : schema.matches(
                                    /[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
                                    'This is not the correct Swift Code'
                              );
                    }),

                    routeCode: Yup.string().when('region', {
                        is: 'USA',
                        then: Yup.string().required('RouteCode is required.'),
                    }),

                    verified: Yup.bool()
0
ChrisJnr Potgieter 23 सितंबर 2020, 13:10

मिक्सी वनऑफ का उपयोग करके यह प्रयास करें

validationSchema={Yup.object().shape({
                    name: Yup.string().min(3).required('Name is required.'),
                    address: Yup.string().required('Address is required.'),
                    country: Yup.string().required('Country is required.'),
                    region: Yup.string().required('Region is required.'),
                    city: Yup.string().required('City is required.'),

                    swiftCode: Yup.string().when('region', {
                        is: oneOf(['Europe', 'Europe', 'other'] as const),
                                                 

                        then: Yup.string()
                            .required('SwiftCode is required.')
                            .matches(
                                /[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?/i,
                                'This is not the correct Swift Code'
                            ),
                    }),

                    routeCode: Yup.string().when('region', {
                        is: 'USA',
                        then: Yup.string().required('RouteCode is required.'),
                    }),

                    verified: Yup.bool(),
                })}
0
HAMZA 19 सितंबर 2020, 21:27