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.

Bug, Feature, or Question?

Feature

Current Behavior

formikBag.submitForm does not consider submit handlers that return promises.

Desired Behavior

formikBag.submitForm should return a Promise (ideally the submit handler’s own Promise) so the developer can handle server API rejections accordingly in cases where it is not possible / desirable to handle them in the original submit handler.

Suggested Solutions

Would require changes in src/Formik.tsx. In it, methods submitForm and executeSubmit.

Additional Information

Commented here #42, but created a new issue for visibility as I think I have a valid use-case. To reiterate, I want to create a custom reusable form component that handles server API validation errors in a generic way.

Small example of what I want to do:

class LoginForm extends React.Component {
  async handleSubmit(values) {
    await axios.post('login', values) // no try/catch here
  }

  render() {
    return (
      <Formik
        onSubmit={this.handleSubmit}
        render={() => (
          <CustomGenericForm>
            ...stuff
          </CustomGenericForm>
        )}
      />
    )
  }
}

class CustomGenericForm extends React.Component {
  handleSubmit = async event => {
    event.preventDefault()
    const { formik } = this.context
    try {
      await formik.submitForm()
    } catch (error) {
      if (error.response && error.response.data && error.response.data.validationErrors) {
        formik.setErrors(error.response.data.validationErrors)
      } else {
        throw error
      }
    } finally {
      formik.setSubmitting(false)
    }
  }

  render() {
    const { children } = this.props
    return <form onSubmit={this.handleSubmit}>{children}</form>
  }
}

CustomGenericForm.contextTypes = {
  formik: PropTypes.object,
}

Or is there a way to achieve what I want with the current version?


  • Formik Version:1.0.0-alpha.2
  • React Version: 16.2

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:29
  • Comments:15 (1 by maintainers)

github_iconTop GitHub Comments

10reactions
jaredpalmercommented, Dec 14, 2018

This is now available in formik@canary

9reactions
Filson14commented, Sep 5, 2018

+1 on that feature. I’m trying to define handleSubmit as a function returning promise. Would like to get benefit from it when triggered submitForm() inside form component. Currently, submitForm returns “a promise”, but it is not that one I expected. Triggering handleSubmit directly (as said in the docs, it should also start submit process) it returns nothing.

I’m not sure if i missed something but I guess it’s a good issue to comment. Any suggestions (even for workaround) are welcome 😉

I saw that there is something developed currently. #500 Any updates about the work status?Long time since the issue has been reported.

Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript async await Submitting a form with onsubmit using ...
I want to sleep the function check_form() for 1 second. If I click on the link, "Test 1" and "Test 2" will be...
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 >
Async Submission Example - Formik
This example demonstrates how to use async/await to submit a Formik form.
Read more >
Submit Form Async with Raw JS - CodePen
Submit Form Async with Raw JS ... form.container(type="post", action="http://httpbin.org/post", onsubmit="return submitForm(this, outraFuncao)").
Read more >
Handling React Form Submit with Redirect & Async/Await for ...
Who This Is for You have a functioning async/await fetch helper function to POST/GET/PATC... Tagged with react, javascript.
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