davidkpiano / react-redux-form

Create forms easily in React with Redux.
https://davidkpiano.github.io/react-redux-form
MIT License
2.07k stars 251 forks source link

Trigger validations manually #1206

Open ulisescarreonalvarez opened 4 years ago

ulisescarreonalvarez commented 4 years ago

The Problem

Hi everyone,

We are working with 'react-redux-form', we have our own validations, might be 'fetch' for request info to an API or just simple regex.

Our problem is that we are fetching some languages con the app, so our erros for the validations are on the (state, props) of our component.

We actualy have a validation like this:

   <Field
                  name="email"
                  component={TextFieldRedux}
                  placeholder={(dataLanguagesRegister) ? ((dataLanguagesRegister.hasOwnProperty('4000000005')) ? (dataLanguagesRegister['4000000005'].phraseLocalizedText) : 'Email') : 'Email'} 
                  label={(dataLanguagesRegister) ? ((dataLanguagesRegister.hasOwnProperty('4000000005')) ? (dataLanguagesRegister['4000000005'].phraseLocalizedText) : 'Email') : 'Email'}
                  required
                  validate={[this.requiredEmail, this.validateEmail]}
                  className={classes.field}
                />

and the validation function works like this:

  requiredEmail(value) {
    const { dataLanguagesRegister } = this.state;
    const { languageReducer } = this.props;
    console.log('Required email function', languageReducer);
    if ((languageReducer.dataLanguagesFetched)) {
      if (languageReducer.dataLanguages.hasOwnProperty('4000000034')) {
        return (value == null ? languageReducer.dataLanguages['4000000034'].phraseLocalizedText : undefined);
      }
    }
    return 'Required';
  }

So on the first place we can show the error on English like: `Email name need to have at least a non free domain.`

We have a dispatch for change the language to show something like:

Παρακαλώ εισάγετε μια έγκυρη διεύθυνση ηλεκτρονικού ταχυδρομείου

So, the text on the validation error STILL shows the english phrase, becase the validation need to be triggered on the first place to change and get the new text.

We need a function to trigger all the validations on the language request, thats possible?

Thanks in advance.