Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to easily identify the validation error if error message handling is not done right. #2265

Open
rwankar opened this issue Jan 9, 2025 · 0 comments

Comments

@rwankar
Copy link

rwankar commented Jan 9, 2025

I'm using latest yup with react-hook-forms and MUI. Sometimes, developers make a mistake of not passing the error to the UI component. If clicking on form submit causes a validation error, the form doesn't submit but there's no "easy" way to know which field is causing the error.

In a recent case, I had a form with about 20 fields. I finally wrote a validation function and then added a Validate button to the form which would log the validation error.

const validateSchemaManually = () => {
  const formValues = getValues();
  schema
    .validate(formValues, { abortEarly: false })
    .then((validatedData) => console.log('Valid Data:', validatedData))
    .catch((err) => console.log('Validation Errors:', err.errors));
};

<FormWrapper>
  <FormProvider {...form}>
    <form onSubmit={handleSubmit(onSubmit)}>
      {/* Form fields */}
      <button type="button" onClick={validateSchemaManually}>
        Validate
      </button>
    </form>
  </FormProvider>
</FormWrapper>;

I was able to find the issue with this.

Is there a way to make yup log the validation error? Or is there a way to provide a callback that will log the error?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant