Skip to content

Commit f6ceec0

Browse files
committed
Align between
1 parent da39169 commit f6ceec0

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

lib/src/form_builder_validators.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,13 +1155,13 @@ class FormBuilderValidators {
11551155
/// or equal to.
11561156
/// - [errorText] The error message when the value is not in the range.
11571157
/// - [checkNullOrEmpty] Whether to check for null or empty values.
1158-
static FormFieldValidator<num> between(
1158+
static FormFieldValidator<T> between<T>(
11591159
num min,
11601160
num max, {
11611161
String? errorText,
11621162
bool checkNullOrEmpty = true,
11631163
}) =>
1164-
BetweenValidator(
1164+
BetweenValidator<T>(
11651165
min,
11661166
max,
11671167
errorText: errorText,

lib/src/numeric/between_validator.dart

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import '../base_validator.dart';
1414
/// - [checkNullOrEmpty] Whether to check if the value is null or empty.
1515
///
1616
/// {@endtemplate}
17-
class BetweenValidator extends BaseValidator<num> {
17+
class BetweenValidator<T> extends BaseValidator<T> {
1818
/// Constructor for the between validator.
1919
const BetweenValidator(
2020
this.min,
@@ -37,9 +37,18 @@ class BetweenValidator extends BaseValidator<num> {
3737
FormBuilderLocalizations.current.betweenErrorText(min, max);
3838

3939
@override
40-
String? validateValue(num valueCandidate) {
41-
final num value = valueCandidate;
42-
if (value < min || value > max) {
40+
String? validateValue(T valueCandidate) {
41+
final num? value;
42+
43+
if (valueCandidate is String) {
44+
value = num.tryParse(valueCandidate);
45+
} else if (valueCandidate is num) {
46+
value = valueCandidate;
47+
} else {
48+
return errorText;
49+
}
50+
51+
if (value == null || (value < min || value > max)) {
4352
return errorText;
4453
}
4554

0 commit comments

Comments
 (0)