Skip to content

Commit

Permalink
docs: add caching example (#78)
Browse files Browse the repository at this point in the history
* docs: add caching example
  • Loading branch information
renancaraujo authored Feb 28, 2023
1 parent acbec34 commit 3abdce8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
29 changes: 29 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,35 @@ void main() {
}
```


## Caching validation results

For cases where the validator method has an expensive implementation, one could cache the validation result on a `late final` field.

```dart
import 'package:formz/formz.dart';
enum EmailValidationError { invalid }
class Email extends FormzInput<String, EmailValidationError> {
Email.pure([super.value = '']) : super.pure();
Email.dirty([super.value = '']) : super.dirty();
static final _emailRegExp = RegExp(
r'^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$',
);
late final validationResultCache =
_emailRegExp.hasMatch(value) ? null : EmailValidationError.invalid;
@override
EmailValidationError? validator(String value) {
return validationResultCache;
}
}
```

[ci_badge]: https://github.com/VeryGoodOpenSource/formz/actions/workflows/main.yaml/badge.svg
[ci_link]: https://github.com/VeryGoodOpenSource/formz/actions
[coverage_badge]: https://raw.githubusercontent.com/VeryGoodOpenSource/formz/main/coverage_badge.svg
Expand Down
15 changes: 10 additions & 5 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,10 @@ class _MyFormState extends State<MyForm> {

class MyFormState with FormzMixin {
MyFormState({
this.email = const Email.pure(),
Email? email,
this.password = const Password.pure(),
this.status = FormzSubmissionStatus.initial,
});
}) : email = email ?? Email.pure();

final Email email;
final Password password;
Expand All @@ -191,23 +191,28 @@ class MyFormState with FormzMixin {
enum EmailValidationError { invalid }

class Email extends FormzInput<String, EmailValidationError> {
const Email.pure([super.value = '']) : super.pure();
const Email.dirty([super.value = '']) : super.dirty();
Email.pure([super.value = '']) : super.pure();

Email.dirty([super.value = '']) : super.dirty();

static final _emailRegExp = RegExp(
r'^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$',
);

late final validationResultCache =
_emailRegExp.hasMatch(value) ? null : EmailValidationError.invalid;

@override
EmailValidationError? validator(String value) {
return _emailRegExp.hasMatch(value) ? null : EmailValidationError.invalid;
return validationResultCache;
}
}

enum PasswordValidationError { invalid }

class Password extends FormzInput<String, PasswordValidationError> {
const Password.pure([super.value = '']) : super.pure();

const Password.dirty([super.value = '']) : super.dirty();

static final _passwordRegex =
Expand Down

0 comments on commit 3abdce8

Please # to comment.