formik: Yup schema.validate() options, show every error of a field at the same time
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(/georges.abitbol@gmail.com/, '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
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 70
- Comments: 33 (1 by maintainers)
I’m also interested in this, I would like to know what are the proposed solutions. I’d like to have something like:
Is it possible?
Would love to see this become an option.
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 I’m also facing this problem. Would you be open to a
validationSchemaOptions-property on theFormik-component? If so Id’ be willing to provide a PR.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
@jaredpalmer could u help us please?
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 wrote a small function to convert the yup error into an error object:
Here’s how I addressed:
where to use this validate function
Got any examples of that?
Step 1: Paste this out of your function
const asyncValidateSchema = (schema) => (values) => schema .validate(values, { abortEarly: false, strict: false, }) .then(() => ({})) .catch(({ inner }) => inner.reduce( (memo, { path, message }) => ({ ...memo, [path]: (memo[path] || []).concat(message), }), {} ) );Step 2: Change Formik validation schema to validate<Formik enableReinitialize={true} onSubmit={(values, actions) => handleSubmit(values, actions) } initialValues={defaultState} validate={asyncValidateSchema(validationSchema)} >Step 3: Now add validation schema outside the functionconst validationSchema = yup.object().shape({ new_password: yup .string() .required() .min(8, "At least 8 characters") .upperCaseCheck() .numberCheck() .specialCharacterCheck() .lowerCaseCheck(), re_new_password: yup .string() .required() .oneOf( [yup.ref("new_password"), null], "The password you entered does not match" ), });I have added some validation scenarios for passwordyup.addMethod(yup.string, "upperCaseCheck", function (errorMessage) { return this.test(oneUpperCase, errorMessage, function (value) { const { path, createError } = this; // change regex here to make other functions if (!/(?=.*[A-Z])/.test(value)) { return createError({ message:At least one uppercase letter, path: path, // Fieldname }); } return true; }); });Now you will get a list of errors insideerrors.new_password. Hope this helps. Cheers!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