Formik: Is there any way to set a hook that will fire if the form has failed the validation?

Created on 1 Jun 2018  路  5Comments  路  Source: formium/formik

I need to fire a function passed down in props if the form (powered by formik obviously) has failed the validation after submit. It should happen only after clicking submit, not on every re-render.
What's the best approach for that?

stale

All 5 comments

652 ist where similar discussion is happening. tl;dr there is no actual support for such functionality in formik, but you can achieve it by setting custom values on submit and manually submitting form (which is hacky but works). I use something like this for example:

        submitForm = (enableValidation: boolean) => {
            const { submitForm, setFieldValue } = this.props;
            setFieldValue('enableValidation', enableValidation);
            setTimeout(() => {
                submitForm();
            });
        }

@latviancoder how exactly can your example help triggering a function call if validation has failed?

Good question..

You click submit button, set custom flag in values, submitForm, then use custom validate function in which you check for your flag and call/not call your function.

The main problem is how to unset your custom flag now. It's almost like you have to use setTimeout to unset it after a short pause, because you don't have an onValuesChanged callback in formik.

Like I said, it feels hacky..

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.

ProBot automatically closed this due to inactivity. Holler if this is a mistake, and we'll re-open it.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Jucesr picture Jucesr  路  3Comments

najisawas picture najisawas  路  3Comments

giulioambrogi picture giulioambrogi  路  3Comments

green-pickle picture green-pickle  路  3Comments

jaredpalmer picture jaredpalmer  路  3Comments