File tree Expand file tree Collapse file tree 4 files changed +23
-0
lines changed Expand file tree Collapse file tree 4 files changed +23
-0
lines changed Original file line number Diff line number Diff line change 43
43
* Add unique
44
44
* Add ISBN
45
45
* Add singleLine
46
+ * Add defaultValue
46
47
47
48
## 10.0.1
48
49
* Fix regression (include l10n files)
Original file line number Diff line number Diff line change @@ -55,6 +55,7 @@ Available built-in helper validators:
55
55
- ` FormBuilderValidators.aggregate() ` - runs the validators in parallel, collecting all errors.
56
56
- ` FormBuilderValidators.log() ` - runs the validator and logs the value at a specific point in the validation chain.
57
57
- ` FormBuilderValidators.skipWhen() ` - runs the validator and skips the validation when a certain condition is met.
58
+ - ` FormBuilderValidators.defaultValue() ` - runs the validator using the default value when the provided value is null.
58
59
59
60
Available built-in type validators include:
60
61
Original file line number Diff line number Diff line change @@ -119,6 +119,14 @@ class FormBuilderValidators {
119
119
};
120
120
}
121
121
122
+ /// [FormFieldValidator] that transforms the value to a default if it's null or empty before running the validator.
123
+ /// * [defaultValue] is the default value to transform to.
124
+ /// * [validator] is the validator to apply.
125
+ static FormFieldValidator <T > defaultValue <T >(
126
+ T defaultValue,
127
+ FormFieldValidator <T > validator,
128
+ ) => (valueCandidate) => validator (valueCandidate ?? defaultValue);
129
+
122
130
/// [FormFieldValidator] that requires the field have a non-empty value.
123
131
/// * [errorText] is the error message to display when the value is empty
124
132
static FormFieldValidator <T > required < T > ({
Original file line number Diff line number Diff line change @@ -1208,4 +1208,17 @@ void main() {
1208
1208
expect (validator ('The quick brown fox\n\r jumps' ), isNotNull);
1209
1209
}),
1210
1210
);
1211
+
1212
+ testWidgets (
1213
+ 'FormBuilderValidators.defaultValue' ,
1214
+ (WidgetTester tester) => testValidations (tester, (context) {
1215
+ final validator = FormBuilderValidators .defaultValue <String >('default' , FormBuilderValidators .alphabetical ());
1216
+ // Pass
1217
+ expect (validator (null ), isNull);
1218
+ expect (validator ('hello' ), isNull);
1219
+ // Fail
1220
+ expect (validator ('123' ), isNotNull);
1221
+ expect (validator ('' ), isNotNull);
1222
+ }),
1223
+ );
1211
1224
}
You can’t perform that action at this time.
0 commit comments