form_bloc icon indicating copy to clipboard operation
form_bloc copied to clipboard

Change language of the error messages

Open angwandi opened this issue 4 years ago • 3 comments

Hey,

How do we go about changing the language of the error messages from: FieldBlocValidatorsErrors.required. The default is in English.

Thanks

angwandi avatar May 29 '20 14:05 angwandi

Hello @angwandi,

Here is my solution until @GiancarloCode answers to you ;-).

I have changed the language of the error by removing all the calls to FieldBlocValidatorsErrors by calling my own methods. This is not the best manner to do but that works.

Example:

class TeamFormBloc extends FormBloc<String, String> {
  final name = TextFieldBloc(
    name: 'nom',
    validators: [
      //FieldBlocValidators.required,
      _required,
    ],
  );
...
  static String _required(String name) {
    //print('_required() - 1');
    if (name == null ||
        name == false ||
        ((name is Iterable || name is String || name is Map) &&
            name.length == 0)) {
      return 'Paramètre obligatoire !';
    }
    return null;
  }
...

cloudystef avatar May 31 '20 10:05 cloudystef

Okay thank you, I will try it.

angwandi avatar Jun 01 '20 07:06 angwandi

just override the defaultErrorBuilder

FieldBlocBuilder.defaultErrorBuilder =
     (BuildContext context, String error, FieldBloc fieldBloc) {
   switch (error) {
     case FieldBlocValidatorsErrors.required:
//        if (fieldBloc is MultiSelectFieldBloc || fieldBloc is SelectFieldBloc) {
////          return 'Please select an option';
////        }
       return Translations.of(context).field_required;

     case FieldBlocValidatorsErrors.email:
       return Translations.of(context).invalid_email;

     case FieldBlocValidatorsErrors.passwordMin6Chars:
       return 'The password must contain at least 6 characters.';

     case FieldBlocValidatorsErrors.confirmPassword:
       return Translations.of(context).err_no_same;

     case CustomFieldBlocValidatorsErrors.input_lenght_10:
       return Translations.of(context).input_lenght_10;

     case CustomFieldBlocValidatorsErrors.input_lenght_8:
       return Translations.of(context).input_lenght_8;

     case CustomFieldBlocValidatorsErrors.input_lenght_6:
       return Translations.of(context).input_lenght_6;

     case CustomFieldBlocValidatorsErrors.input_lenght_4:
       return Translations.of(context).input_lenght_4;

     case CustomFieldBlocValidatorsErrors.password_error:
       return Translations.of(context).password_error;

     case CustomFieldBlocValidatorsErrors.alphaNUmeriqueRegExp:
       return "alphaNUmeriqueRegExp";

     default:
       return error;
   }
 };

devZakariya avatar Jun 14 '20 21:06 devZakariya