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 · 5 comments
Open

Validation Error #3923

Master-Daniel opened this issue Dec 4, 2023 · 5 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

@user4302
Copy link

user4302 commented May 14, 2024

i thought it worked for me too when the condition is true,
but it doesnt show the validation even when the condition is false,
any idea why?
is there a way to console log and find the value in this condition in the console output?

FormValidationSchema: {
    section3: Yup.object().shape({
      heroId: Yup.object().shape({
        hidden: Yup.boolean(),
        detail: Yup.string().when("hidden", {
          is: () => true,
          then: () => Yup.string().notRequired(),
          otherwise: () => Yup.string().required("Hero is required"),
        }),
      }),
    }),
  },

EDIT:
[FIXED]

FormValidationSchema: {
    section3: Yup.object().shape({
      heroId: Yup.object().shape({
        hidden: Yup.boolean(),
        detail: Yup.string()
          .notRequired()
          .when("hidden", {
            is: (val) => val === false,
            then: (schema) => schema.required("Hero is required"),
            otherwise: (schema) => schema.notRequired(),
          }),
      }),
    }),
  },

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

5 participants