Skip to content

Commit

Permalink
Fix field validation causing form to submit without validation
Browse files Browse the repository at this point in the history
  • Loading branch information
dgcohen committed Dec 20, 2024
1 parent c03586a commit 8b2d38d
Showing 1 changed file with 4 additions and 5 deletions.
9 changes: 4 additions & 5 deletions src/components/HoldPages/EDDRequestForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,14 @@ const EDDRequestForm = ({

const validateAndSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
e.preventDefault()
const newValidatedFields = validateEDDForm(eddFormState, invalidFields)

// Validate the form on submission in case the user hasn't typed in all the required fields
setInvalidFields((prevInvalidFields) =>
validateEDDForm(eddFormState, prevInvalidFields)
)
setInvalidFields(newValidatedFields)

// Find the first invalid field and focus on it
const firstInvalidField = invalidFields.find(
(firstInvalidFieldKey) => firstInvalidFieldKey.isInvalid
const firstInvalidField = newValidatedFields.find(
(validatedFieldKey) => validatedFieldKey.isInvalid
)

// Prevent form submission and focus on first invalid field if there is one
Expand Down

0 comments on commit 8b2d38d

Please sign in to comment.