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 ++++++++++++++++++++++++++++++---------- src/features/auth/GuestLogin.tsx | 24 ++++++++++++-------- 2 files changed, 51 insertions(+), 20 deletions(-) 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() { /> - - + + diff --git a/src/features/auth/GuestLogin.tsx b/src/features/auth/GuestLogin.tsx index 1f228d8..cbab494 100644 --- a/src/features/auth/GuestLogin.tsx +++ b/src/features/auth/GuestLogin.tsx @@ -10,8 +10,7 @@ function GuestLogin() { const navigate = useNavigate(); const [login] = useLoginMutation(); - const { register, handleSubmit, - formState: { isDirty, isValid } } = useForm({ + const { register, handleSubmit, formState: { errors } } = useForm({ defaultValues: { firstName: '', lastName: '' @@ -29,7 +28,12 @@ function GuestLogin() { return ( - + Guest Login @@ -38,24 +42,26 @@ function GuestLogin() { variant="outlined" margin="normal" fullWidth - required - {...register("firstName", { required: true })} + error={!!errors.firstName} + helperText={errors.firstName?.message} + {...register("firstName", { required: "Please enter your first name" })} /> -- cgit v1.2.3