Redux form - how to pass UPDATED props to validation function?

China☆狼群 提交于 2019-12-11 08:07:24

问题


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

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