-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Yup schema.validate() options, show every error of a field at the same time #243
Comments
@jaredpalmer I'm also facing this problem. Would you be open to a |
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'm also interested in this, I would like to know what are the proposed solutions. 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): |
I also need to acheive the same, but not able to make it, can you please share the code pen example ? |
This issue is also related: #750 |
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 |
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. |
100%. I have the same situation (create password validation). |
Has anyone found any solution to this? |
@jaredpalmer please post a good work around, if any? |
The solution is to use a custom validate prop for now. We’ll 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. |
If anyone hits this looking for a stopgap until Formik allows schema options, check this out. |
I wrote a small function to convert the yup error into an error object: import { ValidationError } from 'yup';
type ErrorObject = {
[field: string]: string[];
};
/**
* Convert yup error into an error object where the keys are the fields and the values are the errors for that field
* @param {ValidationError} err The yup error to convert
* @returns {ErrorObject} The error object
*/
export function yupErrorToErrorObject(err: ValidationError): ErrorObject {
const object: ErrorObject = {};
err.inner.forEach((x) => {
if (x.path !== undefined) {
object[x.path] = x.errors;
}
});
return object;
} |
Hello, I used validateOnChange: false, into the useFormik() hook. Attached the code:
|
Hi,I need help on One scenario initially, I need submit button to disable so I use the ValidateOnMount it helps me out for the same. The issue is when start typing the text it did not display the error message. code for the error message <TextInput
Formik validationSchema={loginValidationSchema}
can any One Help me out on these |
Step 1: Paste this out of your function |
Got any examples of that? |
For anyone interested in using this solution with either TypeScript or with nested properties within formik using the function FormikAsyncValidateSchema<FormState extends object>(
schema: Yup.ObjectSchema<any>
): ( values: FormState ) => Promise<FormikErrors<FormState>> {
return async function ( values: FormState ): Promise<FormikErrors<FormState>> {
try {
await schema.validate( values, { abortEarly: false, strict: false, } );
return Promise.resolve( {} );
} catch ( yupErr ) {
return yupErr.inner.reduce( ( memo, { path, message } ) => {
return set( memo, path, ( memo[path] || [] ).concat( message ) );
}, {} );
}
};
} this results in the returned object looking like this: const errors = {
parent_obj: {
childFormKey: ['Child Form Key is required']
}
} instead of: const errors = {
"parent_obj.childFormKey": ['Child Form Key is required']
} |
Here's how I addressed: validate={(values) => {
// Return all errors at once
return validationSchema.validate(values, { abortEarly: false })
.then(() => {})
.catch((err) => {
return err.inner.reduce((obj, e) => {
if (!(e.path in obj)) obj[e.path] = []
obj[e.path] = obj[e.path].concat(e.errors)
return obj
}, {})
})
}} |
where to use this validate function |
@Akarshit7 You use it as a prop on |
@Akarshit7 That worked great thanks 😄 Just got to find a way to override the types now 🙃 |
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:
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
The text was updated successfully, but these errors were encountered: