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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

add onValidationFail in <Formik/> #1670

Open
xurei opened this issue Jul 11, 2019 · 4 comments
Open

add onValidationFail in <Formik/> #1670

xurei opened this issue Jul 11, 2019 · 4 comments
Labels

Comments

@xurei
Copy link

xurei commented Jul 11, 2019

馃殌 Feature request

Context

I'm using Formik with yup for validation. I would like to show an alert when validation fails, but only on submit. I didn't find any easy way to do that.

Current Behavior

Right now the best way I found is to write a custom validate() fucntion that basically reproduce the code in Formik and call a function if an error is found.

<Formik validate={(values) => {
    return (
        validateYupSchema(values, options.validationSchema)
        .then(
            () => {
                return null;
            },
            (err) => {
                // Taken from Formik source code
                if (err.name === 'ValidationError') {
                    const errors = yupToFormErrors(err);
                    
                    // This is the part I added
                    if (props.onValidationFail) {
                        this.showAlertBox('Some fields are invalid');
                    }
                    
                    //Throw errors back to formik
                    throw errors;
                }
                else {
                    // We throw any other errors
                    /* ... */
                }
            }
        )
    }
}>

Desired Behavior

Formik could send an event in case of a validation fail. The context of the validation (submitting or not) should be useful as well.

Suggested Solution

<Formik validationSchema={mySchema} onValidationFails={(errors, formikBag) => {
    this.showAlertBox('Some fields are invalid');
}}>

Who does this impact? Who is this for?

This is mostly useful in conjunction with Yup. A custom validation function can already deal with these cases.

@kieranmaine
Copy link

@xurei How did you get this to only fire on the form submit? Using your code it's displaying alerts after onChange and onBlur events.

@xurei
Copy link
Author

xurei commented Aug 14, 2019

@kieranmaine Does it matter with the subject ? I mean it could be useful for onChange and onBlur events too...

@johnrom
Copy link
Collaborator

johnrom commented Sep 11, 2019

@xurei @kieranmaine I think both of these scenarios are perfect candidates for using a Formik Effect, basically creating your own component which listens to formik props and triggers a callback when certain conditions are met. I made an example here:

https://codesandbox.io/s/formik-v2-onvalidationfail-s7zy1

Unfortunately, Formik doesn't always trigger isValidating, so it can't currently be used for onChange or onBlur events. Any thoughts on whether we can/should support something like that @jaredpalmer @Andreyco ?

@stale stale bot added the stale label Nov 10, 2019
@bombillazo
Copy link

Any update on this? Id like to run a function whenever the form validation fails when I press the submit button, and let it be handled by Formik.

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