Skip to content

Commit

Permalink
Merge pull request #60 from netglade/feat/default-translate
Browse files Browse the repository at this point in the history
Add defaultErrorTranslate on model's level
  • Loading branch information
petrnymsa authored Apr 30, 2024
2 parents e634904 + ef85ff3 commit 52b395d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
3 changes: 3 additions & 0 deletions glade_forms/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 2.1.0
- **[Add]**: Add `defaultTranslateError` on model level.

## 2.0.1
- **[Fix]**: Fix `isUri()` to handle URL corectly

Expand Down
8 changes: 8 additions & 0 deletions glade_forms/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 13 additions & 1 deletion glade_forms/lib/src/core/glade_input.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,7 @@ class GladeInput<T> {
String? inputKey,
bool pure = true,
ErrorTranslator<T>? translateError,
DefaultTranslations? defaultTranslations,
ValueComparator<T>? valueComparator,
StringToTypeConverter<T>? valueConverter,
InputDependenciesFactory? dependencies,
Expand All @@ -269,6 +270,7 @@ class GladeInput<T> {
value: value ?? initialValue,
initialValue: initialValue,
translateError: translateError,
defaultTranslations: defaultTranslations,
valueComparator: valueComparator,
valueConverter: valueConverter,
inputKey: inputKey,
Expand All @@ -291,6 +293,7 @@ class GladeInput<T> {
String? inputKey,
bool pure = true,
ErrorTranslator<T>? translateError,
DefaultTranslations? defaultTranslations,
ValueComparator<T>? valueComparator,
StringToTypeConverter<T>? valueConverter,
InputDependenciesFactory? dependencies,
Expand All @@ -306,6 +309,7 @@ class GladeInput<T> {
value: value,
initialValue: initialValue,
translateError: translateError,
defaultTranslations: defaultTranslations,
valueComparator: valueComparator,
valueConverter: valueConverter,
inputKey: inputKey,
Expand All @@ -330,6 +334,7 @@ class GladeInput<T> {
ValidatorFactory<int>? validator,
bool pure = true,
ErrorTranslator<int>? translateError,
DefaultTranslations? defaultTranslations,
ValueComparator<int>? valueComparator,
InputDependenciesFactory? dependencies,
OnChange<int>? onChange,
Expand All @@ -345,6 +350,7 @@ class GladeInput<T> {
validator: validator,
pure: pure,
translateError: translateError,
defaultTranslations: defaultTranslations,
valueComparator: valueComparator,
inputKey: inputKey,
dependencies: dependencies,
Expand All @@ -364,6 +370,7 @@ class GladeInput<T> {
ValidatorFactory<bool>? validator,
bool pure = true,
ErrorTranslator<bool>? translateError,
DefaultTranslations? defaultTranslations,
ValueComparator<bool>? valueComparator,
InputDependenciesFactory? dependencies,
OnChange<bool>? onChange,
Expand All @@ -379,6 +386,7 @@ class GladeInput<T> {
validator: validator,
pure: pure,
translateError: translateError,
defaultTranslations: defaultTranslations,
valueComparator: valueComparator,
inputKey: inputKey,
dependencies: dependencies,
Expand Down Expand Up @@ -522,7 +530,7 @@ class GladeInput<T> {
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.
Expand Down Expand Up @@ -680,6 +688,8 @@ class GladeInput<T> {
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;
Expand All @@ -699,6 +709,8 @@ class GladeInput<T> {
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();
Expand Down
2 changes: 2 additions & 0 deletions glade_forms/lib/src/model/glade_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ abstract class GladeModel extends ChangeNotifier {

bool get isDirty => !isPure;

ErrorTranslator<Object?> 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.
Expand Down
2 changes: 1 addition & 1 deletion glade_forms/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down

0 comments on commit 52b395d

Please sign in to comment.