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

Error : branch is not function in version Yup 1.0 #2020

Closed
anardil opened this issue Jun 5, 2023 · 4 comments
Closed

Error : branch is not function in version Yup 1.0 #2020

anardil opened this issue Jun 5, 2023 · 4 comments

Comments

@anardil
Copy link

anardil commented Jun 5, 2023

Hello,

I try to put conditional validation with Vee-validate and Quasar framework.
It works fine when I use Yup 0.32.11 but when I switched on version > 1.0 for Yup.
I got this error:
Uncaught (in promise) TypeError: branch is not a function
Here is the sanbox => https://codesandbox.io/s/vee-validate-v4-with-quasar-framework-forked-jc7mbk?file=/src/App.vue:0-1967. You will see the error.

But if you select in dependencies the version for Yup 0.32.11, it will works.
Someone can help please?
Thank you.

@jquense
Copy link
Owner

jquense commented Jun 5, 2023

please read the docs, changelog, migration instructions, and try and search past issues before opening up new issues

@jquense jquense closed this as completed Jun 5, 2023
@mortezasabihi
Copy link

@anardil
You need to change the schema to the following:

const schema = yup.object({
    name: yup.string().label("Your name").when("checkbox", {
      is: true,
      then: schema => schema.required(),
      otherwise: schema => schema.optional(),
    }),
    checkbox: yup.boolean().label("If checked put your name below"),
  });

@iqtedar123
Copy link

If its not clear, you need to use function notation for then and otherwise.

@enkhee
Copy link

enkhee commented Jan 18, 2024

  Hello,

It works fine when I use Yup 0.32.11 but when I switched on version > 1.3. for Yup. help
validationSchema={Yup.object({
phone: Yup.string().when("loginType", {
is: "phone",
then: (schema) =>
schema
.required(t("validation_contact_phone_required"))
.checkCustomerPhone(t("validation_phone_type")),
otherwise: Yup.string().nullable(),
}),
customerEmail: Yup.string().when("loginType", {
is: "email",
then: (schema) =>
schema
.email(t("validation_contact_email_type"))
.max(255)
.required(t("validation_contact_email_required")),
otherwise: Yup.string().nullable(),
}),
})} Yup.addMethod(Yup.string, "checkCustomerPhone", function (errorMessage) {
return this.test(checker-phone, errorMessage, function (value) {
const twoNumbers = value?.toString()?.slice(0, 2);
let booleanValue = false;
if (item?.value === "976") {
MglPhoneStart.filter((mgl) => {
mgl === twoNumbers && value?.toString()?.length === 8
? (booleanValue = true)
: "";
});
} else {
booleanValue = true;
}
return booleanValue;
});
});

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

No branches or pull requests

5 participants