From e63d2d31564914bd92cfad531373abb0214cefd5 Mon Sep 17 00:00:00 2001 From: AmirReza Jamali Date: Mon, 13 Apr 2026 13:02:49 +0330 Subject: [PATCH] feat(forms): Enhance form validation and submission handling - Implement real-time validation with `isValid` state in Contact Us and Inquiries forms - Add `trigger` function to validate forms before submission - Disable submit button based on form validity and loading state - Improve user experience by preventing submission of invalid forms --- app/_contact-us/form.tsx | 15 ++++++++++++--- app/_inquires/form.tsx | 15 ++++++++++++--- 2 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/_contact-us/form.tsx b/app/_contact-us/form.tsx index 19a402b..51f1dbd 100644 --- a/app/_contact-us/form.tsx +++ b/app/_contact-us/form.tsx @@ -33,10 +33,11 @@ const ContactUsForm = ({ const { register, handleSubmit, - formState: { errors }, + formState: { errors, isValid }, reset, setError, - } = useForm(); + trigger, + } = useForm({ mode: "onChange" }); const onCaptchaVerify = (token: string) => { setHcaptchaToken(token); @@ -47,6 +48,11 @@ const ContactUsForm = ({ }; const onSubmit = async (data: TContactUsForm) => { + const isFormValid = await trigger(); + if (!isFormValid) { + return; + } + if (!hcaptchaToken) { toast.error("Please complete the captcha verification"); return; @@ -124,7 +130,10 @@ const ContactUsForm = ({ />
-
diff --git a/app/_inquires/form.tsx b/app/_inquires/form.tsx index ca5af3e..7bcad38 100644 --- a/app/_inquires/form.tsx +++ b/app/_inquires/form.tsx @@ -32,10 +32,11 @@ const InquiresForm = ({ const { register, handleSubmit, - formState: { errors }, + formState: { errors, isValid }, reset, setError, - } = useForm(); + trigger, + } = useForm({ mode: "onChange" }); const defaultFields = useMemo( () => [ @@ -78,6 +79,11 @@ const InquiresForm = ({ setHcaptchaToken(null); }; const onSubmit = async (data: TInquiresForm) => { + const isFormValid = await trigger(); + if (!isFormValid) { + return; + } + if (!hcaptchaToken) { toast.error("Please complete the captcha verification"); return; @@ -211,7 +217,10 @@ const InquiresForm = ({ />
-