Skip to content

Commit 96225d7

Browse files
Merge pull request #123 from martijn00/between
Align between
2 parents 5a393fe + 4f55489 commit 96225d7

File tree

4 files changed

+368
-157
lines changed

4 files changed

+368
-157
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## 11.0.1
4+
5+
- Align between validator input types with other validators
6+
37
## 11.0.0
48

59
- Split up validators into smaller pieces

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)