Skip to content

Commit 3d2c837

Browse files
committed
More
1 parent a428c37 commit 3d2c837

20 files changed

+545
-112
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,9 @@
5757
- Add dateFuture
5858
- Add SSN (Social Security Number (USA))
5959
- Add fileName
60+
- Add negative number
61+
- Add positive number
62+
- Add not zero number
6063

6164
## 10.0.1
6265

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ URL, min, max, minLength, maxLength, minWordsCount, maxWordsCount, IP, credit ca
9797
- `FormBuilderValidators.email()` - requires the field's value to be a valid email address.
9898
- `FormBuilderValidators.ip()` - requires the field's value to be a valid IP address.
9999
- `FormBuilderValidators.url()` - requires the field's value to be a valid URL.
100-
- `FormBuilderValidators.portNumber()` - requires the field's to be an valid port number.
101-
- `FormBuilderValidators.macAddress()` - requires the field's to be an valid MAC address.
100+
- `FormBuilderValidators.portNumber()` - requires the field's to be a valid port number.
101+
- `FormBuilderValidators.macAddress()` - requires the field's to be a valid MAC address.
102102

103103
### Numeric validators
104104

@@ -114,6 +114,9 @@ URL, min, max, minLength, maxLength, minWordsCount, maxWordsCount, IP, credit ca
114114
- `FormBuilderValidators.minWordsCount()` - requires the word count of the field's value to be greater than or equal to the provided minimum count.
115115
- `FormBuilderValidators.equalLength()` - requires the length of the field's value to be equal to the provided minimum length.
116116
- `FormBuilderValidators.numeric()` - requires the field's value to be a valid number.
117+
- `FormBuilderValidators.positiveNumber()` - requires the field's to be a positive number.
118+
- `FormBuilderValidators.negativeNumber()` - requires the field's to be a negative number.
119+
- `FormBuilderValidators.notZeroNumber()` - requires the field's to be not a number zero.
117120

118121
### String validators
119122

@@ -129,9 +132,9 @@ URL, min, max, minLength, maxLength, minWordsCount, maxWordsCount, IP, credit ca
129132
- `FormBuilderValidators.creditCard()` - requires the field's value to be a valid credit card number.
130133
- `FormBuilderValidators.creditCardExpirationDate()` - requires the field's value to be a valid credit card expiration date and can check if not expired yet.
131134
- `FormBuilderValidators.creditCardCVC()` - requires the field's value to be a valid credit card CVC number.
132-
- `FormBuilderValidators.iban()` - requires the field's to be an valid IBAN.
133-
- `FormBuilderValidators.bic()` - requires the field's to be an valid BIC.
134-
- `FormBuilderValidators.isbn()` - requires the field's to be an valid ISBN.
135+
- `FormBuilderValidators.iban()` - requires the field's to be a valid IBAN.
136+
- `FormBuilderValidators.bic()` - requires the field's to be a valid BIC.
137+
- `FormBuilderValidators.isbn()` - requires the field's to be a valid ISBN.
135138
- `FormBuilderValidators.uuid()` - requires the field's to be a valid uuid.
136139
- `FormBuilderValidators.colorCode()` - requires the field's value to be a valid color code.
137140

lib/l10n/intl_en.arb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,8 @@
5858
"timeErrorText": "Value must be a valid time.",
5959
"dateMustBeInTheFutureErrorText": "Date must be in the future.",
6060
"dateMustBeInThePastErrorText": "Date must be in the past.",
61-
"fileNameErrorText": "Value must be a valid file name."
61+
"fileNameErrorText": "Value must be a valid file name.",
62+
"negativeNumberErrorText": "Value must be a negative number.",
63+
"positiveNumberErrorText": "Value must be a positive number.",
64+
"notZeroNumberErrorText": "Value must not be zero."
6265
}

lib/src/form_builder_validators.dart

Lines changed: 98 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -164,22 +164,13 @@ class FormBuilderValidators {
164164
bool inclusive = true,
165165
String? errorText,
166166
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;
183174

184175
/// [FormFieldValidator] that requires the field's value to be less than (or equal) to the provided number.
185176
/// This validator checks if the field's value is less than or equal to the given maximum value.
@@ -193,22 +184,55 @@ class FormBuilderValidators {
193184
bool inclusive = true,
194185
String? errorText,
195186
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;
212236

213237
/// [FormFieldValidator] that requires the length of the field's value to be greater than or equal to the provided minimum length.
214238
/// This validator checks if the length of the field's value meets the minimum length requirement.
@@ -384,14 +408,14 @@ class FormBuilderValidators {
384408
///
385409
/// ## Parameters:
386410
/// - [errorText] The error message to display when the number is invalid.
387-
static FormFieldValidator<String> numeric({
411+
static FormFieldValidator<T> numeric<T>({
388412
String? errorText,
389413
bool checkNullOrEmpty = true,
390414
}) =>
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;
395419

396420
/// [FormFieldValidator] that requires the field's value to be a valid integer.
397421
/// This validator checks if the field's value is a valid integer.
@@ -404,10 +428,11 @@ class FormBuilderValidators {
404428
String? errorText,
405429
bool checkNullOrEmpty = true,
406430
}) =>
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;
411436

412437
/// [FormFieldValidator] that requires the field's value to be a valid credit card number.
413438
/// This validator checks if the field's value is a valid credit card number.
@@ -737,36 +762,24 @@ class FormBuilderValidators {
737762
/// This validator checks if the field's value is within the specified numerical range.
738763
///
739764
/// ## 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.
742767
/// - [inclusive] Whether the range is inclusive (default: true).
743768
/// - [errorText] The error message to display when the value is not in the range.
744769
static FormFieldValidator<T> range<T>(
745-
num minValue,
746-
num maxValue, {
770+
num min,
771+
num max, {
747772
bool inclusive = true,
748773
String? errorText,
749774
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;
770783

771784
/// [FormFieldValidator] that requires the field's value to be a bool and true.
772785
/// This validator checks if the field's value is true.
@@ -1099,10 +1112,10 @@ class FormBuilderValidators {
10991112
String? errorText,
11001113
bool checkNullOrEmpty = true,
11011114
}) =>
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;
11061119

11071120
/// [FormFieldValidator] that requires the field's value to be an even number.
11081121
/// This validator checks if the field's value is an even number.
@@ -1113,10 +1126,10 @@ class FormBuilderValidators {
11131126
String? errorText,
11141127
bool checkNullOrEmpty = true,
11151128
}) =>
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;
11201133

11211134
/// [FormFieldValidator] that requires the field's value to be a valid port number.
11221135
/// This validator checks if the field's value is a valid port number.
@@ -1131,19 +1144,12 @@ class FormBuilderValidators {
11311144
String? errorText,
11321145
bool checkNullOrEmpty = true,
11331146
}) =>
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;
11471153

11481154
/// [FormFieldValidator] that requires the field's value to be a valid MAC address.
11491155
/// This validator checks if the field's value is a valid MAC address.
@@ -1233,11 +1239,12 @@ class FormBuilderValidators {
12331239
String? errorText,
12341240
bool checkNullOrEmpty = true,
12351241
}) =>
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;
12411248

12421249
/// [FormFieldValidator] that requires the field's value to be in a list of values.
12431250
/// This validator checks if the field's value is in the given list of values.

lib/src/network/network.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ export 'email_validator.dart';
22
export 'ip_validator.dart';
33
export 'mac_address_validator.dart';
44
export 'phone_number_validator.dart';
5+
export 'port_number_validator.dart';
56
export 'url_validator.dart';
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import '../../localization/l10n.dart';
2+
import '../base_validator.dart';
3+
4+
class PortNumberValidator extends BaseValidator<String> {
5+
PortNumberValidator({
6+
this.min = 0,
7+
this.max = 65535,
8+
9+
/// {@macro base_validator_error_text}
10+
super.errorText,
11+
12+
/// {@macro base_validator_null_check}
13+
super.checkNullOrEmpty,
14+
});
15+
16+
final int min;
17+
18+
final int max;
19+
20+
@override
21+
String get translatedErrorText =>
22+
FormBuilderLocalizations.current.portNumberErrorText(min, max);
23+
24+
@override
25+
String? validateValue(String? valueCandidate) {
26+
final int? port = int.tryParse(valueCandidate!);
27+
if (port == null || port < min || port > max) {
28+
return errorText;
29+
}
30+
return null;
31+
}
32+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,31 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class BetweenValidator extends BaseValidator<num> {
5+
BetweenValidator(
6+
this.min,
7+
this.max, {
8+
/// {@macro base_validator_error_text}
9+
super.errorText,
10+
11+
/// {@macro base_validator_null_check}
12+
super.checkNullOrEmpty,
13+
});
14+
15+
final num min;
16+
final num max;
17+
18+
@override
19+
String get translatedErrorText =>
20+
FormBuilderLocalizations.current.betweenErrorText(min, max);
21+
22+
@override
23+
String? validateValue(num? valueCandidate) {
24+
final num value = valueCandidate!;
25+
if (value < min || value > max) {
26+
return errorText;
27+
}
28+
29+
return null;
30+
}
31+
}

0 commit comments

Comments
 (0)