Formik: Yup schema.validate() options, show every error of a field at the same time

Created on 9 Nov 2017  路  22Comments  路  Source: formium/formik

Hi, sorry I didn't find another issue related to this problem. And since yup has first class support I thought it would be justified to ask about it.

So I'm trying to set an option, mainly here the abortEarly option. My goal is to have a password field with multiple errors displayed and it seems it was the only way. Couldn't find how to do that with validateSchema()

So I did the following instead:

  validate: (values) => {
    const schema = Yup.object().shape({
      email: Yup.string()
        .matches(/[email protected]/, 'cant change email'),
      providerName: Yup.string()
        .required('type your name'),
      password: Yup.string()
        .min(8, 'at least 8 chars')
        .matches(/[a-z]/, 'at least one lowercase char')
        .matches(/[A-Z]/, 'at least one uppercase char')
        .matches(/[a-zA-Z]+[^a-zA-Z\s]+/, 'at least 1 number or special char (@,!,#, etc).'),
      passwordConfirm: Yup.string()
        .equalTo(Yup.ref('password'), 'passwords don't match')
    })

    return schema.validate(values, { abortEarly: false })
      .then(() => {})
      .catch((err) => {
        throw err
      })
  }

This way I can get every error of a single field and map them to my components to display every error of a field at the same time. Isn't there a cleaner way ? Also I think it could be a good use case to showcase ? Thanks

Enhancement in progress

Most helpful comment

I'm also interested in this, I would like to know what are the proposed solutions.
I'd like to have something like:

errors = {
  name: [
    "Too short",
    "Cannot contain numbers",
  ]
}

Is it possible?

All 22 comments

@jaredpalmer I'm also facing this problem. Would you be open to a validationSchemaOptions-property on the Formik-component? If so Id' be willing to provide a PR.

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

:/

I have the similar problem.
I need way to manage yup validation options
Now when i use several rules for one field they all called end return last error message.

I'm also interested in this, I would like to know what are the proposed solutions.
I'd like to have something like:

errors = {
  name: [
    "Too short",
    "Cannot contain numbers",
  ]
}

Is it possible?

Hola! So here's the deal, between open source and my day job and life and what not, I have a lot to manage, so I use a GitHub bot to automate a few things here and there. This particular GitHub bot is going to mark this as stale because it has not had recent activity for a while. It will be closed if no further activity occurs in a few days. Do not take this personally--seriously--this is a completely automated action. If this is a mistake, just make a comment, DM me, send a carrier pidgeon, or a smoke signal.

Comments here keep getting upvotes, so I'm going to post this and hopefully keep this issue open for addressing it in the near future.

@jaredpalmer could u help us please?

Would love to see this become an option.

New PR for the new version of Formik (which is quite different since it now uses hooks):
https://github.com/jaredpalmer/formik/pull/1573

ght it

I also need to acheive the same, but not able to make it, can you please share the code pen example ?
Thanks

@acezard solution don't work for me. Yup returns a ValidationError with packed errors, while Formik expects object with keys.

yupToFormErrors(err) mapping should be called , but it is internal function
https://github.com/jaredpalmer/formik/blob/24ac8ddbddc5717c57178b8a9e90f752fbaa6858/src/Formik.tsx#L216-L217

Agree that we need an option to get a list of all current validation errors at the same time.

Yeah my solution is 3 years old at this point @KrzysztofMadejski, I would not be surprised if it's broken by now.

Sadly it seems this issue has been neglected, but I don't blame the formik owner, open source is open source.

I guess my ticket could be closed maybe, I don't know. Formik has changed a lot since then

@acezard This may be late, But there is a property name inner that contain path & error message
err.inner.path will return something likeuser.[property]

Showing multiple errors at the same time is more friendly. Especially in case of setting a password. Without it, it's bad impression for new users. I really think this need more attention.

@jaredpalmer

Showing multiple errors at the same time is more friendly. Especially in case of setting a password for new users. Without it, it's bad impression for new users. I really think this need more attention.

@jaredpalmer

100%. I have the same situation (create password validation).

Has anyone found any solution to this?
I'm having a similar issue.
While creating a password, I want to know which options passed and which options failed and display the errors on the UI.
Is it possible with Formik and Yup or is there any alternatives?

@jaredpalmer please post a good work around, if any?

The solution is to use a custom validate prop for now. We鈥檒l reevaluate in v3

Weird that my comment got deleted; I guess it's not allowed to mention alternative libraries here. Good news that this feature will be considered for v3 though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

jaredpalmer picture jaredpalmer  路  3Comments

dearcodes picture dearcodes  路  3Comments

green-pickle picture green-pickle  路  3Comments

emartini picture emartini  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments