0
Follow
4
View

No error messages after setting react formik validation

dy198528 注册会员
2023-01-26 04:32

As the error message said you can not create a property telephoneNumber on an empty string '', but you initialized errors as const errors: any = '';

Try to init the errors variable in validate function as described in the formik docs for validate as an empty object e.g. const errors = {};.

Here is a working stackblitz with some of your given code.

Additional example from the formik docs for validate function:

 // Synchronous validation
const validate = (values, props /* only available when using withFormik */) => {
  const errors = {};

  if (!values.email) {
    errors.email = 'Required';
  } else if (!/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
    errors.email = 'Invalid email address';
  }

  //...

  return errors;
};

// Async Validation
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));

const validate = (values, props /* only available when using withFormik */) => {
  return sleep(2000).then(() => {
    const errors = {};
    if (['admin', 'null', 'god'].includes(values.username)) {
      errors.username = 'Nice try';
    }
    // ...
    return errors;
  });
};

About the Author

Question Info

Publish Time
2023-01-26 04:32
Update Time
2023-01-26 04:32

Related Question

error C2065: 'CFileFind'

OneNote UWP版重置后无法登录

pandas.errors错误”.创建数据帧时InvalidIndexError"

zsh: command not found:

No module named 'pyLDAvis'

基础cli错误(npm和node?)

气体估计错误-执行恢复{"originalError": {"code": 3, "data": "0xfb7f5079", "message": "执行恢复"}}

下一个JS 13下一次构建失败时使用appDir错误:' PageNotFoundError:无法找到模块的页面…'

奇怪的错误在nodejs时,请求承诺使用for循环方式?

通过snap在CentOS上安装LXD,但它说“cmmand not found”