问题
I have a couple fields in my component, most of which are explicitly named. However, I have a button that dynamically adds tickets with an id, for example ticket1
, ticket2
, etc. These tickets are stored in an array and available via this.props
In my validate function, however, I am not sure how to reference/loop through the list of tickets and run any validations, mainly because I am not sure how to pass down these updated field values to the validate function. on page load, it takes the current state of props. however, when I add a ticket, update this.props, and click a submit button, the validate function has a prior version of this.props which I cant seem to update.
Here is mine below
const validate = values => {
const errors = {}
if (!values.eventTitle){
console.log('here', values)
errors.eventTitle = 'Event title required'
}
// below, can't do this since I don't know if there tickets in advance
// loop over tickets array and determine errors
// ...
return errors
}
export default validate
and then
const mapStateToProps = (state) => {
return {
tickets: state.event.get('tickets')
}
}
const EventFieldsWrapper = reduxForm({
form: 'EventFields',
validate,
onSubmitFail: (errors) => scrollToFirstError(errors),
})(EventFields)
... connect component and redux-form
来源:https://stackoverflow.com/questions/41272064/redux-form-how-to-pass-updated-props-to-validation-function