davidkpiano / react-redux-form

Create forms easily in React with Redux.
https://davidkpiano.github.io/react-redux-form
MIT License
2.06k stars 252 forks source link

use setErrors to set error for the top level model in the async call in submitFields #1035

Open zzhjerry opened 6 years ago

zzhjerry commented 6 years ago

The Problem

I'm using submitFields to set errors for individual fields. But when an error doesn't match any field, I want to set the error in the errors in the form's errors field.

here's a part of the code I tried (see codepen for full version)

    const login$Q = () => {
      return new Promise((resolve, reject) => {
        setTimeout(() => reject('show this in errors field of user'), 500)
      })
    }
    const loginActionAsync = () => (dispatch) => {
      return login$Q().catch(err => {
        dispatch(actions.setErrors('user', err))
      })
    }
    const { dispatch } = this.props
    dispatch(actions.submitField('user', dispatch(loginActionAsync)))

But it doesn't seem to work. May if it's I didn't do it correctly or it's not possible to do?

Steps to Reproduce

https://codepen.io/zzhjerrygit/pen/gXQjGr?editors=0011

Expected Behavior

I'm able to use setError after the async call finishes

Actual Behavior

I cannot use setError after the async call finishes

Reproducible Code Example

https://codepen.io/zzhjerrygit/pen/gXQjGr?editors=0011

zzhjerry commented 6 years ago

Sorry, don't bother. I should have used

dispatch(actions.submitField('user', dispatch(loginActionAsync())))
zzhjerry commented 6 years ago

Actually, there's another typo. After I fix that, the example in codepen doesn't work again