question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

submitForm seems to not work if validate is async

See original GitHub issue

When calling submitForm and using validate with async validations submitForm never executes


  • Formik Version: ^0.11.10

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
hudecsamuelcommented, Mar 14, 2018

Having the same problem here, the example is a bit confusing. Tried to look at the source code and figure it out.

You have to return a promise (the example is resolving a promise and is either returning nothing or throwing an error)

Edited your codesandbox to have it working as expected here

btw the example could be be something along the lines of (if you really have to return a Promise)

const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))

const validate = (values, props) => sleep(2000)
    .then(() => new Promise((resolve, reject) => {
        const errors = {}
        if (['admin', 'null', 'god'].includes(values.username)) {
            errors.username = 'Nice try'
        }
        // ...
        if (Object.keys(errors).length) {
            reject(errors) // this can be 'throw errors'
        } else {
            resolve()
        }
    }))

it doesn’t really make sense, but it returns a promise in the end that is resolved inside formik.

You can also resolve a Promise like that in the validate function and return the errors as object

const validate = (values, props) => sleep(2000).then(() => {
    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
})

Here is another example of using Promise as return value of validate function that works

const validate = (values, props) => new Promise((resolve, reject) => {
    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'
    }

    if (Object.keys(errors).length) {
        reject(errors)
    } else {
        resolve()
    }
})
1reaction
esperancaJScommented, Mar 14, 2018

Thanks for your availability @prichodko.

I have replicated it here

Based on the example on the README.md

console.log("submiting!");

never triggers

Read more comments on GitHub >

github_iconTop Results From Across the Web

angular async validator not triggered on submit if user does ...
I enter a username and password, submit the form, and get an error such as "this account is not yet valid" from the...
Read more >
Async Form Posts With A Couple Lines Of Vanilla JavaScript
In this tutorial we'll write a tiny JavaScript event handler that will post our HTML forms using fetch instead of the classic synchronous ......
Read more >
Validation - Formik
Formik is designed to manage forms with complex validation with ease. Formik supports synchronous and asynchronous form-level and field-level validation.
Read more >
useForm - setError - React Hook Form
This method will force set isValid formState to false , however, it's important to aware isValid will always be derived by the validation...
Read more >
Handling form submission - Blazor University
The OnSubmit event is executed when the form is submitted, regardless of whether the form passes validation or not. It is possible to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found