question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Passing translated strings to redux-form field level validation

See original GitHub issue

I am not able to come up with a way to translate field level validations in redux-form. In the example below, how can I translate a string outside of the component scope?

const minValue0 = minValue(0)('translatedString')
const TheForm => props => {
 ..        return(<Field
          id="quantity"
          type="number" 
          name="quantity"
          validate={[minValue0]}
          component={InputField} />)

}

Defining minValue0 inside the component does not work because the validation will break.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:17 (8 by maintainers)

github_iconTop GitHub Comments

20reactions
jamuhlcommented, Sep 28, 2017

You can require i18n and use it’s t function outside of components (no translate hoc), sample:

import Validator from 'validatorjs';
import i18n from './i18n';
import { unflatten } from 'flat';

// Check if value is includes in usedValuesString.
// Comparison is performed after passing values through 'normalizer'.
function checkUniqueness(value, usedValuesString, normalizer) {
  const normalizedUsedValues = usedValuesString.split(',').map(normalizer);
  const normalizedValue = normalizer(value);
  return !normalizedUsedValues.includes(normalizedValue);
}

export function validate(rules) {

  return (values) => {
    const commonMessages = i18n.t('validation:validationErrors', { returnObjects: true });
    const customMessages = i18n.t('validation', { returnObjects: true });
    const validator = new Validator(values || {}, rules, { ...commonMessages, ...customMessages });
    validator.passes();
    const errors = {};
    Object.keys(validator.errors.all()).forEach(field => {
      errors[field] = validator.errors.first(field);
    });
    return unflatten(errors);
  };
}
1reaction
ideasOScommented, Sep 1, 2018

I wanted to document a related problem that I have managed to solve. The issue is already closed, but I believe this is the best place for others to find this hint. In my project, I am using redux, React, redux-form FieldArrays, react-select, react-i18next.

I wanted to create a form which was able to create an array as one of the form fields. I did not manage to use a function from the props for the ‘component’ of the FieldArray inside the render function of the form component, but had to create one outside of the render function. That helped me to avoid an infinite loop and is working fine.

Of course it was necessary to import i18n’s t function as @jamuhl recommends https://github.com/i18next/react-i18next/issues/306#issuecomment-332868722 .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux-form Field-Level Validation & error translation with ...
I think that the value of validate props should not change between renders as it causes the field to be re-registered. What is...
Read more >
Field - Redux Form
Allows you to provide a field-level validation rule. The function is given the fields current value, all other form values, the props passed...
Read more >
Field - Redux Form
Allows you to to provide a field-level validation rule. The function will be given the current value of the field, all the other...
Read more >
Field - Redux Form
Props you can pass to Field. name : String [required]. A string path, in dot-and-bracket notation, corresponding to a value in the form...
Read more >
Field-Level Validation Example - Redux Form
The parameters to the validation function are: value - The current value of the field; allValues - The values of the entire form;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found