Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add defaultErrorTranslate on model's level #60

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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