form_bloc
form_bloc copied to clipboard
Change language of the error messages
Hey,
How do we go about changing the language of the error messages from: FieldBlocValidatorsErrors.required
. The default is in English.
Thanks
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;
}
...
Okay thank you, I will try it.
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;
}
};