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

Add a way to display only one error for a field #667

Open
francois-pasquier opened this issue Apr 5, 2024 · 3 comments
Open

Add a way to display only one error for a field #667

francois-pasquier opened this issue Apr 5, 2024 · 3 comments
Labels

Comments

@francois-pasquier
Copy link

Describe the bug

Currently we return field.state.meta.touchedErrors ValidationError[] as a string by concataining the different errors of a field with commas.

It would be great to be able to display only one error.

e.g:

         validators={{
            onChange: v.string([
              v.minLength(1, t(i18n)`This field is mandatory`),
              v.custom(isDateStringValid, t(i18n)`The birth date format is incorrect`),
              v.custom(isDateStringOver18, t(i18n)`You have to be at least 18 years`),
            ]),
          }}

These valibot validators will end up on showing all three errors just by typing a letter and deleting it.
Imo, this is not a good UX.

I would expect some kind of priority in the error messages, thus only displaying the first validation to return an error.

Your minimal, reproducible example

https://github.com/TanStack/form/

Steps to reproduce

     validators={{
        onChange: v.string([
          v.minLength(1, t(i18n)`This field is mandatory`),
          v.custom(isDateStringValid, t(i18n)`The birth date format is incorrect`),
          v.custom(isDateStringOver18, t(i18n)`You have to be at least 18 years`),
        ]),
      }}

Expected behavior

More control over the errors display

How often does this bug happen?

None

Screenshots or Videos

No response

Platform

macOS

TanStack Form adapter

react-form

TanStack Form version

0.19.0

TypeScript version

No response

Additional context

No response

@crutchcorn
Copy link
Member

This should be done at the validator level where you can pass transformErrors to transformErrors: errors => errors[0] rather than transformErrors: errors => errors.join(', ')

Will mark this as "Good first issue" and a feature request

@seobbang
Copy link

@crutchcorn Hello, can I work on this issue? If possible, please assign it to me. 😊

@jessekelly881
Copy link

jessekelly881 commented May 13, 2024

Not sure if this is relevant to this issue or if I should create a separate one but being able to return an array from

validators={{
    onChange: ["err1", "err2"] // string | string[]
}}

instead of having to return a single .join("\n")ed string would be a very nice addition as well.

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

No branches or pull requests

4 participants