简体   繁体   English

无法以redux-form显示提交错误

[英]Cannot display submit errors in redux-form

i'm trying to display submit errors in my form but i cannot figure out why this doesn't work. 我试图在我的表单中显示提交错误,但我无法弄清楚为什么这不起作用。 When i try to submit form with errors i'm getting just simple error in console and cannot submit it again despite that buttons are enabled and no errors are displayed in my form. 当我尝试提交有错误的表单时,我在控制台中只得到简单的错误,并且无法再次提交它,尽管启用了按钮并且我的表单中没有显示错误。

Uncaught (in promise) 未知(承诺)

const validate = values => {
    const errors = {}
    const requiredFields = [ 'email', 'password' ]
    requiredFields.forEach(field => {
        if (!values[ field ]) {
            errors[ field ] = 'Pole wymagane'
        }
    })
    if (values.email && !/^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i.test(values.email)) {
        errors.email = 'Podany adres jest nieprawidłowy'
    }
    return errors
}

function submit(values, dispatch) {
        return new Promise((resolve, reject) => {
            dispatch(actionCreators.loginUser(values.email, values.password))
            .catch( err => {
                reject(err)
            })
        })
    }

class LoginForm extends Component {
    constructor(props) {
        super(props);
        //this.onSubmit = this.onSubmit.bind(this);
    }
    render() {
        const { error, handleSubmit, pristine, reset, submitting } = this.props
        return (
            <form onSubmit={handleSubmit(submit)}>
                <div>
                    <Field name="email" 
                    component={renderTextField} 
                    label="Email"/>
                </div>
                <div>
                    <Field name="password" 
                    component={renderTextField} 
                    type="password"
                    label="Hasło">
                        {error && <strong>{error}</strong>}
                    </Field>
                </div>
                <br/>
                <div>
                    <RaisedButton label="Zaloguj" 
                    primary={true} 
                    type="submit" 
                    disabled={pristine || submitting} />
                    <RaisedButton type="button" 
                    disabled={pristine || submitting} 
                    onClick={reset}>Reset
                    </RaisedButton>
                </div>
                <br />
            </form>
        )
    }
}
reactMixin(LoginForm.prototype, LinkedStateMixin);

const mapStateToProps = (state) => {
    return {
        user: state.user.user,
        isAuthenticated: state.user.isAuthenticated
    }
}
// const mapDispatchToProps = (dispatch) => {
//     return {
//         actions: bindActionCreators(actionCreators, dispatch)
//     }
// }
const form =  reduxForm({
    form: 'LoginForm',
    validate,
    onSubmit: submit
})(LoginForm);

export default connect(mapStateToProps )(form);

As it looks at a glimpse, there's some uncaught error inside the submit() function (as the log says...). 在看一眼时,在submit()函数中有一些未被捕获的错误(正如日志所说的那样......)。

This is because Redux-Form only catches SubmissionError , but not other plain errors. 这是因为Redux-Form只捕获SubmissionError ,但不捕获其他普通错误。

So the solution is to handle the error yourself (for instance dispatch a submissionFailed action inside the reject implementation or a catch block) 所以解决方案是自己处理错误(例如在reject实现或catch块中调度submissionFailed操作)

For instance: 例如:

function submit(values, dispatch) {
    return new Promise((resolve) => {
        dispatch(actionCreators.loginUser(values.email, values.password))
        .catch( err => {
            //reject(err)
            dispatch(someNewErrorAnouncingAction(err))
        })
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM