2017-05-26 27 views
1

當試圖在全球驗證使用的驗證與此代碼:如何在全球驗證用翻譯與終極版形式驗證

errors.fieldMissing = [translate('aor.validation.fieldMissing')]; 

我收到以下錯誤:

Warning: Failed prop type: Invalid prop `errorText` supplied to `TextField`, expected a ReactNode. 

沒有translation(),只要將錯誤設置爲一個字符串數組,一切都按預期工作。

如何在全局驗證中使用translation()?

回答

1

例如(從AOR example posts編輯):

<SimpleForm defaultValue={{ average_note: 0 }} validate={(values, props) => { 
    const errors = {}; 
    ['title', 'teaser'].forEach((field) => { 
     if (!values[field]) { 
      errors[field] = [props.translate('aor.validation.required')]; 
     } 
    }); 
... 

專門翻譯和翻譯道具的簡單例子:

import { translate as translator } from 'admin-on-rest'; 
const Title = translator(({ record, translate }) => <span>{record ? translate('title.product', { name: record.name }) : ''}</span>); 
+0

感謝,認爲沒有的伎倆!爲了澄清,爲什麼'props.translate()'和'translate()'在這種情況下有不同的行爲? – willi

+1

從admin-on-rest導入的翻譯是高階組件。它給它的包裝組件一個翻譯道具。這個翻譯道具做實際的字符串翻譯。你可以這樣做以避免混淆:'從'admin-on-rest''導入{translate {translate as translator}。 – wesley6j