Skip to content

Commit f9c6afa

Browse files
committed
Add some more
1 parent 42ff9ca commit f9c6afa

File tree

3 files changed

+51
-0
lines changed

3 files changed

+51
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
* Add debounce
3939
* Add retry
4040
* Add iban
41+
* Add skipWhen
42+
* Add log
43+
* Add aggregate
4144

4245

4346
## 10.0.1

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Available built-in helper validators:
5454
- `FormBuilderValidators.transform()` - transforms the value before running the validator.
5555
- `FormBuilderValidators.debounce()` - runs the validator after a set time.
5656
- `FormBuilderValidators.retry()` - runs the validator again after failing while waiting a certain time.
57+
- `FormBuilderValidators.aggregate()` - runs the validators in parallel, collecting all errors.
58+
- `FormBuilderValidators.log()` - runs the validator and logs the value at a specific point in the validation chain.
59+
- `FormBuilderValidators.skipWhen()` - runs the validator and skips the validation when a certain condition is met.
5760

5861
Available built-in type validators include:
5962

lib/src/form_builder_validators.dart

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,51 @@ class FormBuilderValidators {
107107
};
108108
}
109109

110+
/// [FormFieldValidator] that runs validators and collects all errors.
111+
/// * [validators] is the list of validators to run.
112+
static FormFieldValidator<T> aggregate<T>(
113+
List<FormFieldValidator<T>> validators,
114+
) {
115+
return (valueCandidate) {
116+
final errors = <String>[];
117+
for (final validator in validators) {
118+
final error = validator(valueCandidate);
119+
if (error != null) {
120+
errors.add(error);
121+
}
122+
}
123+
return errors.isNotEmpty ? errors.join('\n') : null;
124+
};
125+
}
126+
127+
/// [FormFieldValidator] that logs the value at a specific point in the validation chain
128+
/// * [log] is the log message to display
129+
static FormFieldValidator<T> log<T>({
130+
String Function(T? value)? log,
131+
}) {
132+
return (valueCandidate) {
133+
if (log != null) {
134+
debugPrint(log(valueCandidate));
135+
}
136+
return null;
137+
};
138+
}
139+
140+
/// [FormFieldValidator] that skips the validation when a certain condition is met.
141+
/// * [condition] is the condition to check.
142+
/// * [validator] is the validator to skip.
143+
static FormFieldValidator<T> skipWhen<T>(
144+
bool Function(T? value) condition,
145+
FormFieldValidator<T> validator,
146+
) {
147+
return (valueCandidate) {
148+
if (condition(valueCandidate)) {
149+
return null;
150+
}
151+
return validator(valueCandidate);
152+
};
153+
}
154+
110155
/// [FormFieldValidator] that requires the field have a non-empty value.
111156
/// * [errorText] is the error message to display when the value is empty
112157
static FormFieldValidator<T> required<T>({

0 commit comments

Comments
 (0)