Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Fixes issue with short / long text max char validation

Ian MacCallum3fbc2e2b884c
diff --git a/apps/web/ui/partners/groups/design/modals/long-text-field-modal.tsx b/apps/web/ui/partners/groups/design/modals/long-text-field-modal.tsx
index 6a1165e697..dc3444d147 100644
--- a/apps/web/ui/partners/groups/design/modals/long-text-field-modal.tsx
+++ b/apps/web/ui/partners/groups/design/modals/long-text-field-modal.tsx
@@ -9,6 +9,10 @@ import { Controller, useForm } from "react-hook-form";
import { v4 as uuid } from "uuid";
import { z } from "zod";
+const MIN_LENGTH = 1;
+const MAX_LENGTH = 5000;
+const DEFAULT_MAX_LENGTH = 500;
+
type LongTextFieldData = z.infer<
typeof programApplicationFormLongTextFieldSchema
>;
@@ -61,7 +65,7 @@ const formDataForLongTextFieldData = (
data: {
placeholder: longTextFieldData?.data?.placeholder ?? "",
maxLengthEnabled: hasMaxLength,
- maxLength: hasMaxLength ? maxLength : 500,
+ maxLength: hasMaxLength ? maxLength : DEFAULT_MAX_LENGTH,
},
};
};
@@ -77,14 +81,13 @@ function LongTextFieldModalInner({
control,
handleSubmit,
register,
+ unregister,
formState: { errors },
watch,
} = useForm<FormData>({
defaultValues: formDataForLongTextFieldData(defaultValues),
});
- const values = watch();
-
const maxLengthEnabled = watch("data.maxLengthEnabled");
return (
@@ -220,15 +223,33 @@ function LongTextFieldModalInner({
id={`${id}-max-length`}
type="number"
placeholder=""
- {...register("data.maxLength", {
- min: 0,
- max: 2000,
- valueAsNumber: true,
- })}
+ {...(maxLengthEnabled
+ ? register("data.maxLength", {
+ min: {
+ value: MIN_LENGTH,
+ message: `Please enter a number between ${MIN_LENGTH} and ${MAX_LENGTH}`,
+ },
+ max: {
+ value: MAX_LENGTH,
+ message: `Please enter a number between ${MIN_LENGTH} and ${MAX_LENGTH}`,
+ },
+ valueAsNumber: true,
+ })
+ : {})}
autoFocus={!isMobile}
- className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ !!errors.data?.maxLength &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
/>
</div>
+
+ {errors.data?.maxLength?.message && (
+ <div className={cn("ml-1 mt-1 text-xs text-red-500")}>
+ {errors.data?.maxLength.message}
+ </div>
+ )}
</motion.div>
</div>
diff --git a/apps/web/ui/partners/groups/design/modals/short-text-field-modal.tsx b/apps/web/ui/partners/groups/design/modals/short-text-field-modal.tsx
index cf1c8d04ac..c45022c7f9 100644
--- a/apps/web/ui/partners/groups/design/modals/short-text-field-modal.tsx
+++ b/apps/web/ui/partners/groups/design/modals/short-text-field-modal.tsx
@@ -9,6 +9,10 @@ import { Controller, useForm } from "react-hook-form";
import { v4 as uuid } from "uuid";
import { z } from "zod";
+const MIN_LENGTH = 1;
+const MAX_LENGTH = 1000;
+const DEFAULT_MAX_LENGTH = 25;
+
type ShortTextFieldData = z.infer<
typeof programApplicationFormShortTextFieldSchema
>;
@@ -61,7 +65,7 @@ const formDataForShortTextFieldData = (
data: {
placeholder: shortTextFieldData?.data?.placeholder ?? "",
maxLengthEnabled: hasMaxLength,
- maxLength: hasMaxLength ? maxLength : 25,
+ maxLength: hasMaxLength ? maxLength : DEFAULT_MAX_LENGTH,
},
};
};
@@ -77,14 +81,13 @@ function ShortTextFieldModalInner({
control,
handleSubmit,
register,
+ unregister,
formState: { errors },
watch,
} = useForm<FormData>({
defaultValues: formDataForShortTextFieldData(defaultValues),
});
- const values = watch();
-
const maxLengthEnabled = watch("data.maxLengthEnabled");
return (
@@ -220,15 +223,33 @@ function ShortTextFieldModalInner({
id={`${id}-max-length`}
type="number"
placeholder=""
- {...register("data.maxLength", {
- min: 0,
- max: 2000,
- valueAsNumber: true,
- })}
+ {...(maxLengthEnabled
+ ? register("data.maxLength", {
+ min: {
+ value: MIN_LENGTH,
+ message: `Please enter a number between ${MIN_LENGTH} and ${MAX_LENGTH}`,
+ },
+ max: {
+ value: MAX_LENGTH,
+ message: `Please enter a number between ${MIN_LENGTH} and ${MAX_LENGTH}`,
+ },
+ valueAsNumber: true,
+ })
+ : {})}
autoFocus={!isMobile}
- className="block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm"
+ className={cn(
+ "block w-full rounded-md border-neutral-300 text-neutral-900 placeholder-neutral-400 focus:border-neutral-500 focus:outline-none focus:ring-neutral-500 sm:text-sm",
+ !!errors.data?.maxLength &&
+ "border-red-600 focus:border-red-500 focus:ring-red-600",
+ )}
/>
</div>
+
+ {errors.data?.maxLength?.message && (
+ <div className={cn("ml-1 mt-1 text-xs text-red-500")}>
+ {errors.data?.maxLength.message}
+ </div>
+ )}
</motion.div>
</div>