@@ -107,6 +107,51 @@ class FormBuilderValidators {
107
107
};
108
108
}
109
109
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
+
110
155
/// [FormFieldValidator] that requires the field have a non-empty value.
111
156
/// * [errorText] is the error message to display when the value is empty
112
157
static FormFieldValidator <T > required < T > ({
0 commit comments