From 11a665f13fc200086b18ccb3ae3089f3edafe782 Mon Sep 17 00:00:00 2001 From: Kariamos Date: Wed, 26 Jun 2024 16:10:50 +0200 Subject: [PATCH] wip: fixed form validation tests on cap and target input fields --- .../components/campaignForm/FormProvider.tsx | 32 +++++++++++++------ 1 file changed, 22 insertions(+), 10 deletions(-) diff --git a/src/pages/campaigns/components/campaignForm/FormProvider.tsx b/src/pages/campaigns/components/campaignForm/FormProvider.tsx index c74c4904..0d73e095 100644 --- a/src/pages/campaigns/components/campaignForm/FormProvider.tsx +++ b/src/pages/campaigns/components/campaignForm/FormProvider.tsx @@ -163,16 +163,28 @@ const FormProvider = ({ deviceList: yup.array().min(1, "At least one device is required"), browsersList: yup.array(), deviceRequirements: yup.string(), - targetSize: yup.string(), - targetCap: yup.string().when("targetSize", { - is: (val: string) => val && val > 0, - then: yup - .string() - .min( - yup.ref("targetSize"), - "Cap must be at least equal to the number of participants" - ), - }), + targetSize: yup + .string() + .test( + "cap-set-wihtout-target-size", + "Target size must be set", + function (value) { + const { targetCap } = this.parent; + if (!value && targetCap) return false; + return true; + } + ), + targetCap: yup + .string() + .test( + "is-greater-or-equal", + "Cap must be at least equal to the number of participants", + function (value) { + const { targetSize } = this.parent; + if (value && parseInt(value) < parseInt(targetSize)) return false; + return true; + } + ), countries: yup.array(), languages: yup.array(), targetNotes: yup.string(),