From cb2a110ae59636daa19321bac912eebec8520636 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Sat, 2 Mar 2024 10:33:48 -0800 Subject: Fix form validation --- src/components/RsvpForm.tsx | 47 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 36 insertions(+), 11 deletions(-) (limited to 'src/components') diff --git a/src/components/RsvpForm.tsx b/src/components/RsvpForm.tsx index 8c67139..448401e 100644 --- a/src/components/RsvpForm.tsx +++ b/src/components/RsvpForm.tsx @@ -19,7 +19,7 @@ function RsvpForm() { const [updateGuest] = useUpdateGuestMutation(); const guest: Guest = useOutletContext(); - const { register, handleSubmit, control } = useForm({ + const { register, handleSubmit, control, formState: { errors } } = useForm({ defaultValues: { id: guest?.id, firstName: guest?.firstName, @@ -48,9 +48,15 @@ function RsvpForm() { RSVP Deadline: - - - Will you attend our wedding? + + + + Will you attend? + )} /> - + @@ -78,17 +84,27 @@ function RsvpForm() { label="Email" variant="outlined" fullWidth - required - {...register("email")} + error={!!errors.email} + helperText={errors.email?.message} + {...register("email", + { required: "Please enter your email address", + pattern: { value: /\S+@\S+\.\S+/, + message: "Please enter a valid email address" } + })} /> @@ -102,8 +118,17 @@ function RsvpForm() { /> - - + + -- cgit v1.2.3