无法从异步提交验证中的道具获取错误值

时间:2017-11-12 18:37:43

标签: reactjs redux react-redux redux-form redux-thunk

我在react-redux应用程序中的提交操作如下

<form onSubmit={handleSubmit(this.onFormSubmit.bind(this))}>
                        <Field
                            name="username"
                            lable="Username"
                            type="text"
                            component={ renderField }
                        />
                        <Field
                            name="email"
                            type="email"
                            lable="Email"
                            component={ renderField }
                        />
                        {console.log("cheki...",error)}
                        hahahaah{error}
                        {error  && <strong>{error}</strong>}
                        <Field
                            name="password"
                            type="password"
                            lable="Password"
                            component={ renderField }
                        />
                        <Field
                            name="confirm_password"
                            type="password"
                            lable="Confirm Password"
                            component={ renderField }
                        />
                        <button type="submit" className="btn btn-primary" disabled={submitting}>Register</button>
                        <Link className="btn btn-danger" to="/login">Cancel</Link>

                    </form>

如果电子邮件已经注册,控制台会在这种情况下成功打印错误。但是,当我尝试在用户表单上打印相同的错误时,如下所示

const {error, submitting, handleSubmit} = this.props;

永远不会打印错误。 即function two(msg){...错误变量始终为空。有什么我做错了吗?

1 个答案:

答案 0 :(得分:0)

首先,如果您只想onSubmit进行验证,则应使用Submit Validation代替。

如果您想要的是异步验证,我可以在您的代码中看到两个错误。

  1. 你没有抛出错误;
  2. 
        catch((error) => {
          console.log('error', error.response);
          // you should throw an error here, for example:
          // throw { username: 'That username is taken' }
          reject(error.response)
        })
    
    
    1. 您的error错误。你应该这样做:
    2. 
          const {
            meta: { asyncValidating, touched, error },
            submitting,
            handleSubmit } = this.props;
      
      

      请参阅redux-form文档中的this great example

相关问题