javascript vuejs 3 yup类型符合确认

piwo6bdm  于 2022-12-21  发布在  Java
关注(0)|答案(1)|浏览(197)

你好我有个小问题
也就是说,我将进行验证,但我希望根据成员类型进行验证,因此如果是公司,则税号是强制性的,但电话号码不是
如果它是个人的,那就正好相反
电话验证值为假时如果值为真,则仅验证税

setup() {
const value = ref(false);
const validationSchema = Yup.object().shape({
  phone: Yup.string().trim().required().label("Phone"),
  tax: Yup.string().trim().required().label("Tax"),
});

const { handleSubmit } = useForm({ validationSchema });
const submit = handleSubmit(async (values) => {
  console.log("personName:", values);
});
return {
  validationSchema,
  submit,
  value,
};

},

8yoxcaq7

8yoxcaq71#

memberType添加到验证中,然后您可以执行以下操作:

const validationSchema = Yup.object().shape({
  memberType: Yup.string().required(),
  phone: Yup
    .string()
    .trim()
    .label("Phone")
    .when("memberType", {
        is: "individual",
        then: Yup.string().required("Must enter phone number")
     }),
  tax: Yup
    .string()
    .trim()
    .label("Tax")
    .when("memberType", {
        is: "corporate",
        then: Yup.string().required("Must enter tax number")
     }),
});

相关问题