2017-08-18 57 views
0

我想在提交之前驗證表單,並在底部顯示常見錯誤,而不是將錯誤附加到特定字段。 errros.fieldname =「年齡> 10」。 沒有錯誤,會提示消息繼續提交,然後確認後將調用表單提交。在提交之前還原表單預驗證

使用redux-forms的正確方法是什麼?

BeforeValidate(values) 
    { 
    if (<<some validation condition>>) { 
     this.setState({err:"Error"}) 
     return; 
    } else 
    { 
     this.setState({DialogOpen:true, err:""}) 
    } 
    } 

要顯示常見的錯誤消息,而不是附接作爲error.text 某處呈現功能

&nbsp;{ this.state.err !='' && 
          <span className="text text-danger"><strong>No Value selected! 
</strong></span> 
        } 

該問題的值未在BeforeValidate函數定義。以redux形式提供的同步驗證不提供設置常見錯誤消息的方法。不確定異步驗證是否正確。 所以我被抓住了。請幫忙。

回答

1

您可以使用正常的validate功能並添加_error。整個表單不是字段將是錯誤的。

const validate = ({ options }) => { 
    const errors = {}; 
    if (options.length === 0) errors._error = 'required'; 
    return errors; 
}; 

在您的形式render使用this.props.error檢查形狀誤差。 事情是這樣的:

render() { 
    const { handleSubmit, onSubmit, submitting, error, submitFailed } = this.props; 
    return (
     <View> 
     {error && 
      <FormValidationMessage> 
      {I18n.t(error)} 
      </FormValidationMessage>} 
     </View> 
    ); 
    } 
+0

您也可以返回一個對象,比如'{字段名:「需要」}',它會連接到一個字段中的錯誤。 –

相關問題