diff --git a/app/_inquires/form.tsx b/app/_inquires/form.tsx index 8711a42..a154186 100644 --- a/app/_inquires/form.tsx +++ b/app/_inquires/form.tsx @@ -79,8 +79,35 @@ const InquiresForm = ({ setHcaptchaToken(null); }; const onSubmit = async (data: TInquiresForm) => { - const isFormValid = await trigger(); + const sanitizedData = Object.fromEntries( + Object.entries(data).map(([key, value]) => [ + key, + typeof value === "string" ? value.trim() : value, + ]), + ) as TInquiresForm; + + const requiredFields = fields.filter((field) => field.required); + let hasEmptyRequiredField = false; + + requiredFields.forEach((field) => { + const value = sanitizedData[field.name]; + if (!value) { + setError(field.name, { + type: "required", + message: `${field.label} is required`, + }); + hasEmptyRequiredField = true; + } + }); + + if (hasEmptyRequiredField) { + toast.error("Please complete all required fields"); + return; + } + + const isFormValid = await trigger(formFieldNames); if (!isFormValid) { + toast.error("Please correct invalid form fields"); return; } @@ -91,7 +118,10 @@ const InquiresForm = ({ setIsLoading(true); try { const response = ( - await api.post("inquiries", { ...data, hcaptcha_token: hcaptchaToken }) + await api.post("inquiries", { + ...sanitizedData, + hcaptcha_token: hcaptchaToken, + }) ).data; toast.success(response.message); reset(); @@ -224,7 +254,7 @@ const InquiresForm = ({