Skip to content

Commit bc3523f

Browse files
committed
Add some
1 parent e8b7d34 commit bc3523f

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@
3434
* Add contains
3535
* Add between
3636
* Add inList
37+
* Add transform
38+
* Add debounce
39+
* Add retry
40+
3741

3842
## 10.0.1
3943
* Fix regression (include l10n files)

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ Available built-in helper validators:
5151
- `FormBuilderValidators.compose()` - runs each validator against the value provided.
5252
- `FormBuilderValidators.conditional()` - conditionally runs a validator against the value provided.
5353
- `FormBuilderValidators.or()` - runs each validator against the value provided and passes when any works.
54+
- `FormBuilderValidators.transform()` - transforms the value before running the validator.
55+
- `FormBuilderValidators.debounce()` - runs the validator after a set time.
56+
- `FormBuilderValidators.retry()` - runs the validator again after failing while waiting a certain time.
5457

5558
Available built-in type validators include:
5659

lib/src/form_builder_validators.dart

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import '../form_builder_validators.dart';
35

@@ -43,6 +45,62 @@ class FormBuilderValidators {
4345
};
4446
}
4547

48+
/// [FormFieldValidator] that transforms the value before applying the validator.
49+
static FormFieldValidator transform<T>(
50+
FormFieldValidator<T> validator,
51+
T Function(T? value) transformer,
52+
) {
53+
return (valueCandidate) {
54+
final transformedValue = transformer(valueCandidate);
55+
return validator(transformedValue);
56+
};
57+
}
58+
59+
/// [FormFieldValidator] that debounces the validation.
60+
/// * [duration] is the duration to wait before running the validation.
61+
static FormFieldValidator<T> debounce<T>({
62+
required Duration duration,
63+
required FormFieldValidator<T> validator,
64+
}) {
65+
Timer? debounceTimer;
66+
String? result;
67+
68+
return (valueCandidate) {
69+
debounceTimer?.cancel();
70+
debounceTimer = Timer(duration, () {
71+
result = validator(valueCandidate);
72+
});
73+
74+
return result;
75+
};
76+
}
77+
78+
/// [FormFieldValidator] that retries the validation.
79+
/// * [times] is the number of times to retry the validation.
80+
/// * [duration] is the duration to wait before retrying the validation.
81+
static FormFieldValidator<T> retry<T>({
82+
required int times,
83+
required Duration duration,
84+
required FormFieldValidator<T> validator,
85+
}) {
86+
int retries = 0;
87+
String? result;
88+
89+
return (valueCandidate) {
90+
if (retries < times) {
91+
result = validator(valueCandidate);
92+
if (result != null) {
93+
retries++;
94+
Future.delayed(duration, () {
95+
result = validator(valueCandidate);
96+
});
97+
}
98+
}
99+
100+
return result;
101+
};
102+
}
103+
46104
/// [FormFieldValidator] that requires the field have a non-empty value.
47105
static FormFieldValidator<T> required<T>({
48106
String? errorText,

0 commit comments

Comments
 (0)