问题
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