2017-03-18 78 views
1

我正在使用redux-form,並且只希望在調度redux-form提交操作後基於正在按下的按鈕運行驗證。此刻,我通過調度我自己的操作並使用shouldValidate()函數的nextProps根據狀態中的值返回true或false來更改點擊按鈕時的狀態。如何更改基於狀態更改的redux-form shouldValidate選項

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 ===「保存」解析爲真和shouldValidate函數返回false,表單驗證功能仍然運行。我希望表單在點擊「保存」按鈕時提交但不驗證,但在其他時間提交和驗證,但它似乎不能動態?

回答

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));