Redux-Form will not submit if more than one Field

断了今生、忘了曾经 提交于 2019-12-11 16:48:32

问题


Why does my redux-form not submit when I have more than one field?

If I have more than one field then the onSubmit on my form does not run.

The following code will not show the alert :

//@flow
import * as React from 'react';
import {Field, reduxForm, Form} from 'redux-form';


class CustomerPage2 extends React.Component {

    constructor(props) {
        super(props);
    }

    render() {

        let submit = () => alert("show me the money")

        return (
                <Form id="myform" onSubmit={submit} >
                        <Field
                            label={'asdf'}
                            className={'input'}
                            id='1'
                            name={'salutation'}
                            mandatory={true}
                            component='input'
                        />
                        <Field
                            label={'asdf2'}
                            className={'input'}
                            id='2'
                            name={'first_name'}
                            mandatory={true}
                            component='input'
                        />
                </Form>
        );
    }
}

export default reduxForm({
    form: 'customerRegistration',
})(CustomerPage2)

However if I remove one of the fields the alert will pop up :

render() {

let submit = () => alert("show me the money")

return (
        <Form id="myform" onSubmit={submit} >
                <Field
                    label={'asdf'}
                    className={'input'}
                    id='1'
                    name={'salutation'}
                    mandatory={true}
                    component='input'
                />
        </Form>
);

}

I also created a fiddle where you can see it for your own eyes :

https://jsfiddle.net/036ur33k/150/

Just remove one of the fields and you will see what I mean.


回答1:


I think you forgot to use the handleSubmit function (redux-form adds it on the component props) in your onSubmit event.

I modified your fiddle, check if it is what you need.

https://jsfiddle.net/036ur33k/173/



来源:https://stackoverflow.com/questions/54086825/redux-form-will-not-submit-if-more-than-one-field

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!