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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v2]: Explore adding debounceValidateMs prop #1526

Open
jaredpalmer opened this issue May 18, 2019 · 4 comments
Open

[v2]: Explore adding debounceValidateMs prop #1526

jaredpalmer opened this issue May 18, 2019 · 4 comments

Comments

@jaredpalmer
Copy link
Owner

This prop would take a number as a prop and internally Formik would debounce all non-submit-related validation calls.

@jannikbuschke
Copy link

+1, would totally like to see that

@alexweber
Copy link

Any updates on this?

@stale stale bot removed the stale label Jun 22, 2020
@yongzhi-chen
Copy link

+1. this is a really nice to have feature

@johnrom
Copy link
Collaborator

johnrom commented Nov 30, 2020

I think validation can be a little more granular than this. Along with the theme of creating "bindings" for validators like Yup, I think we can also create default bindings for the default Formik validators -- FormValidator and FieldValidator, then we can compose this and let users wrap it with whatever they want to. This is how I'd handle it in my V3 reducer refs branch:

import { FormValidator, FieldValidator } from 'formik';
import { YupValidator } from '@formik/yup';

type ValidationHandler<Values extends FormikValues> = (
  getState: () => FormikState, 
  dispatch: React.Dispatch<FormikMessage>
) => Promise<FormikErrors<Values>>;

export const defaultValidator: ValidationHandler<Values> = (getState, dispatch) => {
    return Promise.all([
      FormValidator(getState, dispatch),
      FieldValidator(getState, dispatch),
      YupValidator(getState, dispatch),
   ]);
};

// in Formik
const runAllValidations = const useEventCallback(
  () => (props.validationHandler ?? defaultValidator)(getState, dispatch), 
  [props.validationHandler, defaultValidator, getState, dispatch]
);

// in userland
import { Formik, defaultValidationHandler } from 'formik';

const myForm = () => {
  const validationHandler = useDebounce(defaultValidationHandler, 300);

  return <Formik validationHandler={validationHandler} validate={() => console.log('this API doesn't change, it is what FormValidator uses')} />
}

(eventually we would deprecate YupValidator from defaultValidator, and @formik/yup would expose a YupValidationHandler which includes the YupValidator)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants