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
This commit is contained in:
AmirReza Jamali
2026-04-13 13:02:49 +03:30
parent 2229ffcd7a
commit e63d2d3156
2 changed files with 24 additions and 6 deletions
+12 -3
View File
@@ -33,10 +33,11 @@ const ContactUsForm = ({
const { const {
register, register,
handleSubmit, handleSubmit,
formState: { errors }, formState: { errors, isValid },
reset, reset,
setError, setError,
} = useForm<TContactUsForm>(); trigger,
} = useForm<TContactUsForm>({ mode: "onChange" });
const onCaptchaVerify = (token: string) => { const onCaptchaVerify = (token: string) => {
setHcaptchaToken(token); setHcaptchaToken(token);
@@ -47,6 +48,11 @@ const ContactUsForm = ({
}; };
const onSubmit = async (data: TContactUsForm) => { const onSubmit = async (data: TContactUsForm) => {
const isFormValid = await trigger();
if (!isFormValid) {
return;
}
if (!hcaptchaToken) { if (!hcaptchaToken) {
toast.error("Please complete the captcha verification"); toast.error("Please complete the captcha verification");
return; return;
@@ -124,7 +130,10 @@ const ContactUsForm = ({
/> />
</div> </div>
<div className={`${colSpanClass} flex justify-end`}> <div className={`${colSpanClass} flex justify-end`}>
<button className="bg-(--primary) cursor-pointer w-fit text-white rounded-full py-4 px-6 flex justify-center items-center text-center"> <button
type="submit"
disabled={!isValid || isLoading}
className="bg-(--primary) cursor-pointer w-fit text-white rounded-full py-4 px-6 flex justify-center items-center text-center disabled:opacity-60 disabled:cursor-not-allowed">
{isLoading ? <Loading /> : buttonText} {isLoading ? <Loading /> : buttonText}
</button> </button>
</div> </div>
+12 -3
View File
@@ -32,10 +32,11 @@ const InquiresForm = ({
const { const {
register, register,
handleSubmit, handleSubmit,
formState: { errors }, formState: { errors, isValid },
reset, reset,
setError, setError,
} = useForm<TInquiresForm>(); trigger,
} = useForm<TInquiresForm>({ mode: "onChange" });
const defaultFields = useMemo( const defaultFields = useMemo(
() => [ () => [
@@ -78,6 +79,11 @@ const InquiresForm = ({
setHcaptchaToken(null); setHcaptchaToken(null);
}; };
const onSubmit = async (data: TInquiresForm) => { const onSubmit = async (data: TInquiresForm) => {
const isFormValid = await trigger();
if (!isFormValid) {
return;
}
if (!hcaptchaToken) { if (!hcaptchaToken) {
toast.error("Please complete the captcha verification"); toast.error("Please complete the captcha verification");
return; return;
@@ -211,7 +217,10 @@ const InquiresForm = ({
/> />
</div> </div>
<div className={`${colSpanClass} flex justify-end`}> <div className={`${colSpanClass} flex justify-end`}>
<button className="bg-(--primary) cursor-pointer w-fit text-white rounded-full py-4 px-6"> <button
type="submit"
disabled={!isValid || isLoading}
className="bg-(--primary) cursor-pointer w-fit text-white rounded-full py-4 px-6 disabled:opacity-60 disabled:cursor-not-allowed">
{isLoading ? <Loading /> : submitText} {isLoading ? <Loading /> : submitText}
</button> </button>
</div> </div>