Search code examples
translationadmin-on-rest

How to use translation in global validation with redux-form validation


When trying to use validation in a global validator with this code:

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

I get the following error:

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

Without the translation(), just setting the error to an array of a string, everything works as expected.

How can translation() be used in global validation?


Solution

  • For example(edited from 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')];
            }
        });
    ...
    

    A short example of translate hoc and translate props:

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