如何根据更改状态更改redux-form shouldValidate选项

时间:2017-03-18 18:06:47

标签: reactjs react-redux redux-form

我目前正在使用redux-form,只希望在调度redux-form提交操作后,根据正在按下的按钮运行验证。目前,我通过调度自己的动作并使用shouldValidate()函数的nextProps来改变状态,并根据状态中的值返回true或false。

BriefForm = reduxForm({
  form: "brief", // a unique name for this form
  destroyOnUnmount: false,
  forceUnregisterOnUnmount: false,
  enableReinitialize: true,
  keepDirtyOnReinitialize: false,
  shouldValidate(params) {
    if (params.nextProps && params.nextProps.submitButton === "save") {
      return false
    } else {
      return true
    }
  },
  validate
})(BriefForm);

但是,即使params.nextProps.submitButton ===“save”解析为true且shouldValidate函数返回false,表单的验证函数仍会运行。我希望表单在单击“保存”按钮时提交但不验证,但在其他时候提交和验证,但似乎它不能动态?

1 个答案:

答案 0 :(得分:1)

您是否尝试过使用'react-redux'中的connectshouldValidate传递给reduxForm()?像这样:

const mapStateToProps = state => ({
  shouldValidate: ({ nextProps: getNextProps(state) }) => {
      return nextProps && nextProps.submitButton === "save"
    }
})

BriefForm = connect(
  mapStateToProps
)(reduxForm({
  form: "brief", // a unique name for this form
  destroyOnUnmount: false,
  forceUnregisterOnUnmount: false,
  enableReinitialize: true,
  keepDirtyOnReinitialize: false,
  validate
})(BriefForm));