@@ -164,22 +164,13 @@ class FormBuilderValidators {
164
164
bool inclusive = true ,
165
165
String ? errorText,
166
166
bool checkNullOrEmpty = true ,
167
- }) {
168
- return (T ? valueCandidate) {
169
- if (valueCandidate != null ) {
170
- assert (valueCandidate is num || valueCandidate is String );
171
- final num ? number = valueCandidate is num
172
- ? valueCandidate
173
- : num .tryParse (valueCandidate.toString ());
174
-
175
- if (number != null && (inclusive ? number < min : number <= min)) {
176
- return errorText ??
177
- FormBuilderLocalizations .current.minErrorText (min);
178
- }
179
- }
180
- return null ;
181
- };
182
- }
167
+ }) =>
168
+ MinValidator <T >(
169
+ min,
170
+ inclusive: inclusive,
171
+ errorText: errorText,
172
+ checkNullOrEmpty: checkNullOrEmpty,
173
+ ).validate;
183
174
184
175
/// [FormFieldValidator] that requires the field's value to be less than (or equal) to the provided number.
185
176
/// This validator checks if the field's value is less than or equal to the given maximum value.
@@ -193,22 +184,55 @@ class FormBuilderValidators {
193
184
bool inclusive = true ,
194
185
String ? errorText,
195
186
bool checkNullOrEmpty = true ,
196
- }) {
197
- return (T ? valueCandidate) {
198
- if (valueCandidate != null ) {
199
- assert (valueCandidate is num || valueCandidate is String );
200
- final num ? number = valueCandidate is num
201
- ? valueCandidate
202
- : num .tryParse (valueCandidate.toString ());
203
-
204
- if (number != null && (inclusive ? number > max : number >= max)) {
205
- return errorText ??
206
- FormBuilderLocalizations .current.maxErrorText (max);
207
- }
208
- }
209
- return null ;
210
- };
211
- }
187
+ }) =>
188
+ MaxValidator <T >(
189
+ max,
190
+ inclusive: inclusive,
191
+ errorText: errorText,
192
+ checkNullOrEmpty: checkNullOrEmpty,
193
+ ).validate;
194
+
195
+ /// [FormFieldValidator] that requires the field's value to be a positive number.
196
+ ///
197
+ /// ## Parameters:
198
+ /// - [errorText] The error message to display when the value is not positive.
199
+ /// - [checkNullOrEmpty] Whether to check for null or empty values (default: true).
200
+ static FormFieldValidator <T > positiveNumber <T >({
201
+ String ? errorText,
202
+ bool checkNullOrEmpty = true ,
203
+ }) =>
204
+ PositiveNumberValidator <T >(
205
+ errorText: errorText,
206
+ checkNullOrEmpty: checkNullOrEmpty,
207
+ ).validate;
208
+
209
+ /// [FormFieldValidator] that requires the field's value to be a negative number.
210
+ ///
211
+ /// ## Parameters:
212
+ /// - [errorText] The error message to display when the value is not negative.
213
+ /// - [checkNullOrEmpty] Whether to check for null or empty values (default: true).
214
+ static FormFieldValidator <T > negativeNumber <T >({
215
+ String ? errorText,
216
+ bool checkNullOrEmpty = true ,
217
+ }) =>
218
+ NegativeNumberValidator <T >(
219
+ errorText: errorText,
220
+ checkNullOrEmpty: checkNullOrEmpty,
221
+ ).validate;
222
+
223
+ /// [FormFieldValidator] that requires the field's value to be a number that is not zero.
224
+ ///
225
+ /// ## Parameters:
226
+ /// - [errorText] The error message to display when the value is zero.
227
+ /// - [checkNullOrEmpty] Whether to check for null or empty values (default: true).
228
+ static FormFieldValidator <T > notZeroNumber <T >({
229
+ String ? errorText,
230
+ bool checkNullOrEmpty = true ,
231
+ }) =>
232
+ NotZeroNumberValidator <T >(
233
+ errorText: errorText,
234
+ checkNullOrEmpty: checkNullOrEmpty,
235
+ ).validate;
212
236
213
237
/// [FormFieldValidator] that requires the length of the field's value to be greater than or equal to the provided minimum length.
214
238
/// This validator checks if the length of the field's value meets the minimum length requirement.
@@ -384,14 +408,14 @@ class FormBuilderValidators {
384
408
///
385
409
/// ## Parameters:
386
410
/// - [errorText] The error message to display when the number is invalid.
387
- static FormFieldValidator <String > numeric ({
411
+ static FormFieldValidator <T > numeric < T > ({
388
412
String ? errorText,
389
413
bool checkNullOrEmpty = true ,
390
414
}) =>
391
- ( String ? valueCandidate) => valueCandidate ? .isNotEmpty == true &&
392
- null == num . tryParse (valueCandidate ! )
393
- ? errorText ?? FormBuilderLocalizations .current.numericErrorText
394
- : null ;
415
+ NumericValidator < T >(
416
+ errorText : errorText,
417
+ checkNullOrEmpty : checkNullOrEmpty,
418
+ ).validate ;
395
419
396
420
/// [FormFieldValidator] that requires the field's value to be a valid integer.
397
421
/// This validator checks if the field's value is a valid integer.
@@ -404,10 +428,11 @@ class FormBuilderValidators {
404
428
String ? errorText,
405
429
bool checkNullOrEmpty = true ,
406
430
}) =>
407
- (String ? valueCandidate) => valueCandidate? .isNotEmpty == true &&
408
- null == int .tryParse (valueCandidate! , radix: radix)
409
- ? errorText ?? FormBuilderLocalizations .current.integerErrorText
410
- : null ;
431
+ IntegerValidator (
432
+ radix: radix,
433
+ errorText: errorText,
434
+ checkNullOrEmpty: checkNullOrEmpty,
435
+ ).validate;
411
436
412
437
/// [FormFieldValidator] that requires the field's value to be a valid credit card number.
413
438
/// This validator checks if the field's value is a valid credit card number.
@@ -737,36 +762,24 @@ class FormBuilderValidators {
737
762
/// This validator checks if the field's value is within the specified numerical range.
738
763
///
739
764
/// ## Parameters:
740
- /// - [minValue ] The minimum value that the field's value should be greater than or equal to.
741
- /// - [maxValue ] The maximum value that the field's value should be less than or equal to.
765
+ /// - [min ] The minimum value that the field's value should be greater than or equal to.
766
+ /// - [max ] The maximum value that the field's value should be less than or equal to.
742
767
/// - [inclusive] Whether the range is inclusive (default: true).
743
768
/// - [errorText] The error message to display when the value is not in the range.
744
769
static FormFieldValidator <T > range <T >(
745
- num minValue ,
746
- num maxValue , {
770
+ num min ,
771
+ num max , {
747
772
bool inclusive = true ,
748
773
String ? errorText,
749
774
bool checkNullOrEmpty = true ,
750
- }) {
751
- return (T ? valueCandidate) {
752
- if (valueCandidate != null ) {
753
- assert (valueCandidate is num || valueCandidate is String );
754
- final num ? number = valueCandidate is num
755
- ? valueCandidate
756
- : num .tryParse (valueCandidate.toString ());
757
-
758
- final String ? minResult =
759
- min (minValue, inclusive: inclusive, errorText: errorText)(number);
760
- final String ? maxResult =
761
- max (maxValue, inclusive: inclusive, errorText: errorText)(number);
762
-
763
- if (minResult != null || maxResult != null ) {
764
- return errorText ?? minResult ?? maxResult;
765
- }
766
- }
767
- return null ;
768
- };
769
- }
775
+ }) =>
776
+ RangeValidator <T >(
777
+ min,
778
+ max,
779
+ inclusive: inclusive,
780
+ errorText: errorText,
781
+ checkNullOrEmpty: checkNullOrEmpty,
782
+ ).validate;
770
783
771
784
/// [FormFieldValidator] that requires the field's value to be a bool and true.
772
785
/// This validator checks if the field's value is true.
@@ -1099,10 +1112,10 @@ class FormBuilderValidators {
1099
1112
String ? errorText,
1100
1113
bool checkNullOrEmpty = true ,
1101
1114
}) =>
1102
- ( String ? valueCandidate) =>
1103
- valueCandidate ? .isEmpty != false || ! isOddNumber (valueCandidate ! )
1104
- ? errorText ?? FormBuilderLocalizations .current.oddNumberErrorText
1105
- : null ;
1115
+ OddNumberValidator (
1116
+ errorText : errorText,
1117
+ checkNullOrEmpty : checkNullOrEmpty,
1118
+ ).validate ;
1106
1119
1107
1120
/// [FormFieldValidator] that requires the field's value to be an even number.
1108
1121
/// This validator checks if the field's value is an even number.
@@ -1113,10 +1126,10 @@ class FormBuilderValidators {
1113
1126
String ? errorText,
1114
1127
bool checkNullOrEmpty = true ,
1115
1128
}) =>
1116
- ( String ? valueCandidate) => valueCandidate ? .isEmpty != false ||
1117
- ! isEvenNumber (valueCandidate ! )
1118
- ? errorText ?? FormBuilderLocalizations .current.evenNumberErrorText
1119
- : null ;
1129
+ EvenNumberValidator (
1130
+ errorText : errorText,
1131
+ checkNullOrEmpty : checkNullOrEmpty,
1132
+ ).validate ;
1120
1133
1121
1134
/// [FormFieldValidator] that requires the field's value to be a valid port number.
1122
1135
/// This validator checks if the field's value is a valid port number.
@@ -1131,19 +1144,12 @@ class FormBuilderValidators {
1131
1144
String ? errorText,
1132
1145
bool checkNullOrEmpty = true ,
1133
1146
}) =>
1134
- (String ? valueCandidate) {
1135
- if (valueCandidate? .isNotEmpty == true ) {
1136
- final int ? port = int .tryParse (valueCandidate! );
1137
- if (port == null || port < min || port > max) {
1138
- return errorText ??
1139
- FormBuilderLocalizations .current.portNumberErrorText (min, max);
1140
- }
1141
- } else {
1142
- return errorText ??
1143
- FormBuilderLocalizations .current.portNumberErrorText (min, max);
1144
- }
1145
- return null ;
1146
- };
1147
+ PortNumberValidator (
1148
+ min: min,
1149
+ max: max,
1150
+ errorText: errorText,
1151
+ checkNullOrEmpty: checkNullOrEmpty,
1152
+ ).validate;
1147
1153
1148
1154
/// [FormFieldValidator] that requires the field's value to be a valid MAC address.
1149
1155
/// This validator checks if the field's value is a valid MAC address.
@@ -1233,11 +1239,12 @@ class FormBuilderValidators {
1233
1239
String ? errorText,
1234
1240
bool checkNullOrEmpty = true ,
1235
1241
}) =>
1236
- (num ? valueCandidate) =>
1237
- valueCandidate == null || valueCandidate < min || valueCandidate > max
1238
- ? errorText ??
1239
- FormBuilderLocalizations .current.betweenErrorText (min, max)
1240
- : null ;
1242
+ BetweenValidator (
1243
+ min,
1244
+ max,
1245
+ errorText: errorText,
1246
+ checkNullOrEmpty: checkNullOrEmpty,
1247
+ ).validate;
1241
1248
1242
1249
/// [FormFieldValidator] that requires the field's value to be in a list of values.
1243
1250
/// This validator checks if the field's value is in the given list of values.
0 commit comments