foxhound87 / mobx-react-form

Reactive MobX Form State Management
https://foxhound87.github.io/mobx-react-form
MIT License
1.1k stars 129 forks source link

How to define/override validation error messages text? #49

Closed joneyphair closed 7 years ago

foxhound87 commented 8 years ago

Hi @joneyphair, it depends on which plugin you are using.

const plugins = {
  dvr: {
    package: validatorjs,
    extend: ($validator) => {
      // here we can access the `validatorjs` instance
      var messages = $validator.getMessages('en');
      messages.required = 'Whoops, :attribute field is required.';
      $validator.setMessages('en', messages);
    },
  },
};

I will improve the documentation about custom error messages in the future.

joneyphair commented 7 years ago

thx

timarney commented 7 years ago

Is there a way to set a generic message for all error types on a field or do you need to override each type i.e. show Invalid Password for all these (required|string|between).

foxhound87 commented 7 years ago

I think you can achieve that using .invalidate('a generic error message') on Fields instances when they don't pass validation (for example observing Field's isValid prop). The invalidate method will overwrite the last error message.

cgat commented 7 years ago

For the DVR case, it seems to be impossible to set custom message messages for the conditional rules (require_if) because a) it doesn't appear to be possible to make conditional rules with the register rule method and b) custom messages are passed into the constructor, which we don't have access to.

jonasdumas commented 6 years ago

If you use DVR Validator , to automatically change the language for all errors, you can simply do it like this:

import validatorjs from 'validatorjs';
import MobxReactForm from 'mobx-react-form';
import { observer } from 'mobx-react';

// For french For example:
validatorjs.useLang('fr');
foxhound87 commented 6 years ago

Related to #305