jaredpalmer / formik

Build forms in React, without the tears 😭
https://formik.org
Apache License 2.0
34.02k stars 2.79k forks source link

How to make get updated errors object onBlur when using validationSchema? #1693

Open irunfourtytwok opened 5 years ago

irunfourtytwok commented 5 years ago

I'm using validationSchema of formik and the validationOnBlur is set to true. I want to make an API call onBlur based on the result of validation on that field. I tried running both the Formik's field OnBlur and my custom onBlur (for making an API call) but the errors object returns empty for the very first time in the custom onBlur function.

import { Field } from 'formik';

<Field
      {...props}
      render={({ field, form: { errors, touched } }) => (
<CustomComponent
          {...props}
          {...field}
          value={field.value || ''}
          onBlur={(e) => {
            if (field.onBlur) {
             //Formik's field onBlur that runs validation
              field.onBlur(e);
            }
            if (props.onBlur) {
             //custom onBlur which is going to make the API call.
              props.onBlur(e);
            }

What's the best possible way to get the updated error object or figure out that there is an error before making an API call?

geocine commented 4 years ago

I am wondering how to do this as well, if I just pass the errors , it will not be the updated errors object