Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validation Error #3923

Open
Master-Daniel opened this issue Dec 4, 2023 · 4 comments
Open

Validation Error #3923

Master-Daniel opened this issue Dec 4, 2023 · 4 comments

Comments

@Master-Daniel
Copy link

Master-Daniel commented Dec 4, 2023

The validation schema below gave me the error Unhandled Runtime Error TypeError: branch is not a function

const settingsFormik = useFormik({
initialValues: {
type: '',
account: '',
opening_balance: '',
account_name: '',
account_number: ''
},
validationSchema: Yup.object().shape({
type: Yup.string().required('Account type is required'),
account: Yup.string(), // Optional field
opening_balance: Yup.number().typeError('Opening balance must be a number').required('Opening balance is required else enter 0'),
account_number: Yup.string().when('type', {
is: 'bank',
then: Yup.string().required('Account number is required'),
otherwise: Yup.string().notRequired(),
}),
account_name: Yup.string().when('type', {
is: 'bank',
then: Yup.string().required('Account name is required'),
otherwise: Yup.string().notRequired(),
})
})
})

@PatrycjuszNowaczyk
Copy link

PatrycjuszNowaczyk commented Dec 6, 2023

Hey @Master-Daniel . My team run into same issue. Solution was quite simple. W use Formik with YUP for validation schema. We have upgraded both, and then error occurs. I had to downgrade YUP library to previous version, due to incompatibility between versions pre v1.0 and versions 1... At the moment of writing it looks like it works. There is a link to YUP NPM https://www.npmjs.com/package/yup.

@rushmata
Copy link

rushmata commented Jan 5, 2024

Hey @Master-Daniel I ran into this error just yesterday, and I solved the problem by converting the "is", "then" and "otherwise" clausules into an arrow function that returns the desired value. This would work in your case:

const settingsFormik = useFormik({
        initialValues: {
            type: '',
            account: '',
            opening_balance: '',
            account_name: '',
            account_number: ''
        },
        validationSchema: Yup.object().shape({
            type: Yup.string().required('Account type is required'),
            account: Yup.string(), // Optional field
            opening_balance: Yup.number().typeError('Opening balance must be a number').required('Opening balance is required else enter 0'),
            account_number: Yup.string().when('type', {
                is: () => 'bank',
                then: () => Yup.string().required('Account number is required'),
                otherwise: () => Yup.string().notRequired(),
            }),
            account_name: Yup.string().when('type', {
                is: () => 'bank',
                then: () => Yup.string().required('Account name is required'),
                otherwise: () => Yup.string().notRequired(),
            })
        })
    })

@MalikOwais123
Copy link

@rushmata thanks for the Perfect solution, It works on latest version as well.

@Master-Daniel
Copy link
Author

Master-Daniel commented Apr 19, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants