Skip to content

Commit 271d732

Browse files
fix(form): enhance error handling and status display in CommonForm and ContactForm
Signed-off-by: Manishnemade12 <mnemade140@gmail.com>
1 parent 9359f93 commit 271d732

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

src/components/CommonForm/index.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,19 @@ const CommonForm = ({ form, title, submit_title, submit_body, submit_button_titl
9696
role: role,
9797
form: form,
9898
}}
99-
onSubmit={async (values) => {
99+
onSubmit={async (values, { setStatus }) => {
100100
if (isValidRole(values.role) && isValidEmail(values.email)) {
101101
const { success } = await submitForm({ memberFormOne: values });
102102
if (success) {
103+
setStatus(null);
103104
setMemberFormOne(values);
104105
setStepNumber(1);
105106
setSubmit(true);
106107
// window.scrollTo(0,window.scrollY - 800);
107108
// confirmationMessageRef.current.scrollIntoView({ behavior: 'smooth' })
108109
scrollElementIntoView(confirmationMessageRef.current, navBarOffset);
109110
} else {
110-
alert("Submission failed. Please try again.");
111+
setStatus({ submitError: "Submission failed. Please try again." });
111112
}
112113
} else {
113114
// if (!(values.google || values.github || values.twitter || values.linkedin)) {
@@ -128,7 +129,11 @@ const CommonForm = ({ form, title, submit_title, submit_body, submit_button_titl
128129
}
129130
}}
130131
>
131-
<Form className="form" method="post">
132+
{({ status }) => (
133+
<Form className="form" method="post" aria-describedby="common-form-status">
134+
<div id="common-form-status" className="form-error" role="alert" aria-live="assertive">
135+
{status?.submitError || ""}
136+
</div>
132137
<label htmlFor="firstname" className="form-name">First Name <span className="required-sign">*</span></label>
133138
<Field type="text" className="text-field" id="firstname" name="firstname" maxLength="32" pattern="^[\p{L}]+('[\p{L}]*)?(?:-[\p{L}]+)*([\s][\p{L}]+('[\p{L}]*)?(?:-[\p{L}]+)*){0,31}$" required />
134139
<label htmlFor="lastname" className="form-name">Last Name <span className="required-sign">*</span></label>
@@ -288,6 +293,7 @@ const CommonForm = ({ form, title, submit_title, submit_body, submit_button_titl
288293

289294
<Button $secondary className="btn" title={submit_button_title || "Submit"} />
290295
</Form>
296+
)}
291297
</Formik>
292298
</div>
293299
}

src/components/ContactForm/index.js

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,23 @@ const ContactForm = () => {
3636
scope: "",
3737
form: "contact",
3838
}}
39-
onSubmit={async (values) => {
39+
onSubmit={async (values, { setStatus }) => {
4040
const { success } = await submitForm({ memberFormOne: values });
4141
if (success) {
42+
setStatus(null);
4243
setmemberFormOne(values);
4344
setSubmit(true);
4445
window.scrollTo(0, 700);
4546
} else {
46-
alert("Submission failed. Please try again.");
47+
setStatus({ submitError: "Submission failed. Please try again." });
4748
}
4849
}}
4950
>
50-
<Form className="form" method="post">
51+
{({ status }) => (
52+
<Form className="form" method="post" aria-describedby="contact-form-status">
53+
<div id="contact-form-status" className="form-error" role="alert" aria-live="assertive">
54+
{status?.submitError || ""}
55+
</div>
5156
<div className="title">
5257
<img className="layer5-logo" src={logo} alt="Layer5 Logo" />
5358
</div>
@@ -209,7 +214,8 @@ const ContactForm = () => {
209214
/>
210215
</div>
211216
</div>
212-
</Form>
217+
</Form>
218+
)}
213219
</Formik>
214220
</div>
215221
</ContactFormWrapper>

0 commit comments

Comments
 (0)