diff --git a/apps/portal/src/components/offers/constants.ts b/apps/portal/src/components/offers/constants.ts index 0dc44da5..bb0f45ed 100644 --- a/apps/portal/src/components/offers/constants.ts +++ b/apps/portal/src/components/offers/constants.ts @@ -14,6 +14,7 @@ export const emptyOption = { export enum FieldError { NON_NEGATIVE_NUMBER = 'Please fill in a non-negative number in this field.', NUMBER = 'Please fill in a number in this field.', + POSITIVE_NUMBER = 'Please fill in a positive number in this field.', REQUIRED = 'Please fill in this field.', } diff --git a/apps/portal/src/components/offers/offersSubmission/submissionForm/BackgroundForm.tsx b/apps/portal/src/components/offers/offersSubmission/submissionForm/BackgroundForm.tsx index df34d81d..2b9f3a4c 100644 --- a/apps/portal/src/components/offers/offersSubmission/submissionForm/BackgroundForm.tsx +++ b/apps/portal/src/components/offers/offersSubmission/submissionForm/BackgroundForm.tsx @@ -6,6 +6,7 @@ import { FieldError } from '~/components/offers/constants'; import type { BackgroundPostData } from '~/components/offers/types'; import { CURRENCY_OPTIONS } from '~/utils/offers/currency/CurrencyEnum'; +import { validatePositiveNumber } from '~/utils/offers/form'; import { EducationFieldOptions } from '../../EducationFields'; import { EducationLevelOptions } from '../../EducationLevels'; @@ -47,7 +48,7 @@ function YoeSection() { label="Specific YOE 1" type="number" {...register(`background.specificYoes.0.yoe`, { - min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 }, + validate: validatePositiveNumber, valueAsNumber: true, })} /> @@ -63,7 +64,7 @@ function YoeSection() { label="Specific YOE 2" type="number" {...register(`background.specificYoes.1.yoe`, { - min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 }, + validate: validatePositiveNumber, valueAsNumber: true, })} /> @@ -146,6 +147,7 @@ function FullTimeJobFields({ defaultCurrency }: FullTimeJobFieldsProps) { type="number" {...register(`background.experiences.0.durationInMonths`, { min: { message: FieldError.NON_NEGATIVE_NUMBER, value: 0 }, + validate: validatePositiveNumber, valueAsNumber: true, })} /> diff --git a/apps/portal/src/utils/offers/form.tsx b/apps/portal/src/utils/offers/form.tsx index ac03e281..1a4b3590 100644 --- a/apps/portal/src/utils/offers/form.tsx +++ b/apps/portal/src/utils/offers/form.tsx @@ -1,5 +1,7 @@ /* eslint-disable @typescript-eslint/no-explicit-any */ +import { FieldError } from '~/components/offers/constants'; + /** * Removes empty objects, empty strings, `null`, `undefined`, and `NaN` values from an object. * Does not remove empty arrays. @@ -85,3 +87,13 @@ export function removeInvalidMoneyData(object: any) { }); return object; } + +export function validatePositiveNumber(v?: number | null) { + return ( + v === null || + v === undefined || + v !== v || + v > 0 || + FieldError.POSITIVE_NUMBER + ); +}