diff --git a/glade_forms/CHANGELOG.md b/glade_forms/CHANGELOG.md index 51c04e2..22945d9 100644 --- a/glade_forms/CHANGELOG.md +++ b/glade_forms/CHANGELOG.md @@ -1,3 +1,6 @@ +## 2.1.0 +- **[Add]**: Add `defaultTranslateError` on model level. + ## 2.0.1 - **[Fix]**: Fix `isUri()` to handle URL corectly diff --git a/glade_forms/README.md b/glade_forms/README.md index adcfe1e..c171879 100644 --- a/glade_forms/README.md +++ b/glade_forms/README.md @@ -355,6 +355,14 @@ Use `defaultTranslation` to provide default translations for common error such a ![translation-example](https://raw.githubusercontent.com/netglade/glade_forms/main/glade_forms/doc/translation.gif) +Or use `defaultErrorTranslate` on model's level. + +Order of translation is as follows: + +```dart +translateError -> defaultTranslation -> `Model.defaultErrorTranslate` -> `error.devMessage` +``` + ### Converters As noted before, if `T` is not a String, a converter from String to `T` has to be provided. diff --git a/glade_forms/lib/src/core/glade_input.dart b/glade_forms/lib/src/core/glade_input.dart index 339b208..ac87902 100644 --- a/glade_forms/lib/src/core/glade_input.dart +++ b/glade_forms/lib/src/core/glade_input.dart @@ -254,6 +254,7 @@ class GladeInput { String? inputKey, bool pure = true, ErrorTranslator? translateError, + DefaultTranslations? defaultTranslations, ValueComparator? valueComparator, StringToTypeConverter? valueConverter, InputDependenciesFactory? dependencies, @@ -269,6 +270,7 @@ class GladeInput { value: value ?? initialValue, initialValue: initialValue, translateError: translateError, + defaultTranslations: defaultTranslations, valueComparator: valueComparator, valueConverter: valueConverter, inputKey: inputKey, @@ -291,6 +293,7 @@ class GladeInput { String? inputKey, bool pure = true, ErrorTranslator? translateError, + DefaultTranslations? defaultTranslations, ValueComparator? valueComparator, StringToTypeConverter? valueConverter, InputDependenciesFactory? dependencies, @@ -306,6 +309,7 @@ class GladeInput { value: value, initialValue: initialValue, translateError: translateError, + defaultTranslations: defaultTranslations, valueComparator: valueComparator, valueConverter: valueConverter, inputKey: inputKey, @@ -330,6 +334,7 @@ class GladeInput { ValidatorFactory? validator, bool pure = true, ErrorTranslator? translateError, + DefaultTranslations? defaultTranslations, ValueComparator? valueComparator, InputDependenciesFactory? dependencies, OnChange? onChange, @@ -345,6 +350,7 @@ class GladeInput { validator: validator, pure: pure, translateError: translateError, + defaultTranslations: defaultTranslations, valueComparator: valueComparator, inputKey: inputKey, dependencies: dependencies, @@ -364,6 +370,7 @@ class GladeInput { ValidatorFactory? validator, bool pure = true, ErrorTranslator? translateError, + DefaultTranslations? defaultTranslations, ValueComparator? valueComparator, InputDependenciesFactory? dependencies, OnChange? onChange, @@ -379,6 +386,7 @@ class GladeInput { validator: validator, pure: pure, translateError: translateError, + defaultTranslations: defaultTranslations, valueComparator: valueComparator, inputKey: inputKey, dependencies: dependencies, @@ -522,7 +530,7 @@ class GladeInput { void updateValueWhenNotNull(T? value, {bool shouldTriggerOnChange = true}) { if (value == null) return; - return updateValue(value, shouldTriggerOnChange: shouldTriggerOnChange); + updateValue(value, shouldTriggerOnChange: shouldTriggerOnChange); } /// Resets input into pure state. @@ -680,6 +688,8 @@ class GladeInput { return translateErrorTmp(err, err.key, err.devErrorMessage, dependenciesFactory()); } else if (defaultConversionMessage != null) { return defaultConversionMessage; + } else if (this._bindedModel case final model?) { + return model.defaultErrorTranslate(err, err.key, err.devErrorMessage, dependenciesFactory()); } return err.devErrorMessage; @@ -699,6 +709,8 @@ class GladeInput { if (defaultTranslationsTmp != null && (e.isNullError || e.hasStringEmptyOrNullErrorKey || e.hasNullValueOrEmptyValueKey)) { return defaultTranslationsTmp.defaultValueIsNullOrEmptyMessage ?? e.toString(); + } else if (this._bindedModel case final model?) { + return model.defaultErrorTranslate(e, e.key, e.devErrorMessage, dependenciesFactory()); } return e.toString(); diff --git a/glade_forms/lib/src/model/glade_model.dart b/glade_forms/lib/src/model/glade_model.dart index c06310f..459e47f 100644 --- a/glade_forms/lib/src/model/glade_model.dart +++ b/glade_forms/lib/src/model/glade_model.dart @@ -16,6 +16,8 @@ abstract class GladeModel extends ChangeNotifier { bool get isDirty => !isPure; + ErrorTranslator get defaultErrorTranslate => (error, key, devMessage, dependencies) => devMessage; + /// Currently tracked inputs by GladeModel. /// /// Be aware that on initialize() these input are binded to model. Later included inputs are not automatically binded. diff --git a/glade_forms/pubspec.yaml b/glade_forms/pubspec.yaml index affd6a9..d8fe8a0 100644 --- a/glade_forms/pubspec.yaml +++ b/glade_forms/pubspec.yaml @@ -1,6 +1,6 @@ name: glade_forms description: A universal way to define form validators with support of translations. -version: 2.0.1 +version: 2.1.0 repository: https://github.com/netglade/glade_forms issue_tracker: https://github.com/netglade/glade_forms/issues screenshots: