2016-11-10 76 views
1

我有一個需要一些自定義onBlur功能的字段,但是當我實現我的功能時,正常的同步驗證停止工作。當我使用componentDidUpdate控制日誌時,仍然可以看到在props.meta.error中更新的錯誤,但實際上並未顯示錯誤。如何在實現自定義onBlur時與Redux-form v6保持同步驗證?

我試過修改手動調度'touch'動作,並嘗試asyncValidation,但錯誤字段不顯示,除非我離開onBlur道具一個人。

有沒有一種方法可以實現我自己的onBlur函數,同時仍然保留原始的錯誤驗證內容?

這就是我正在做的組件:

class PostalCodeTextInput extends React.Component { // eslint-disable-line 
    fetchAddressesValid =() => { 
    // this.props.async(); 
    if (this.props.input.value.length > 5) { 
     if (!this.props.meta.error) { 
     this.props.fetchAddresses(this.props.input.value); 
     } 
    } 
    } 

    render() { 
    const { helpText, input, label, type, meta: { touched, error } } = this.props; 
    const classNames = classnames({ 
     'text-input': 'text-input', 
     'form-group': 'form-group', 
     invalid: touched && error, 
    }); 
    return (
     <div className={classNames}> 
     <label htmlFor={label}>{label}</label> 
     <div> 
      <input 
      {...input} 
      type={type} 
      placeholder={label} 
      onBlur={this.fetchAddressesValid} // * 
      /> 
      {touched && error && <p className="warning">{error}</p>} 
     </div> 
     <small><em>{helpText}</em></small> 
     </div> 
    ); 
    } 
} 

這是一個本應適用的驗證:

const validate = (values) => { // eslint-disable-line 
    const errors = {}; 

    // NewAddressFields 
    if (!values.get('postal_code')) { 
    errors.postal_code = 'Required'; 
    } ... 

這是個什麼樣子,當我註釋掉一行:

// onBlur={this.fetchAddressesValid} 

enter image description here

這是個什麼樣子,當我離開的線,如:

onBlur={this.fetchAddressesValid} 

enter image description here

如何獲得錯誤信息彈出像第一張圖片,同時還用我自己的onblur功能?

回答

1

完成後委託onBlur。


變化

onBlur={this.fetchAddressesValid} 

onBlur={(e) => { 
this.fetchAddressesValid 
input.onBlur(e); 
}} 
+0

如果你只是使用實施例中設置了'Field',喜歡的默認方式你會如何做? 這裏'輸入'是不確定的,而且似乎沒有辦法,可以使用'輸入'。訪問該領域的'onBlur'? – geoboy

+0

以相同的方式進行委託......希望您明白。 '常量MyComponent的=(場)=> { 返回<輸入{... field.input}}的onblur = {(E)=> { this.fetchAddressesValid field.input.onBlur(E); }} /> } ' – lustoykov

+0

Thx!沒有授權的任何方式來做到這一點?如果能夠將其作爲「Field」本身的一個支柱,那會更加清潔嗎? – geoboy