Skip to content

Commit b362433

Browse files
implement isInThePast validator
1 parent a005cb1 commit b362433

File tree

2 files changed

+67
-2
lines changed

2 files changed

+67
-2
lines changed

lib/new_api_prototype/datetime_validators.dart

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ Validator<DateTime> isAfter(
2222
}
2323

2424
/// This function returns a validator that checks if the user [DateTime] input is
25-
/// in the future. If the checking results true, the validator returns `null`.
26-
/// Otherwise, it returns `isInTheFutureMsg`, if provided, or
25+
/// in the future (after [DateTime.now]). If the checking results true, the validator
26+
/// returns `null`. Otherwise, it returns `isInTheFutureMsg`, if provided, or
2727
/// `FormBuilderLocalizations.current.dateMustBeInTheFutureErrorText`.
2828
Validator<DateTime> isInTheFuture({
2929
String? isInTheFutureMsg,
@@ -57,6 +57,21 @@ Validator<DateTime> isBefore(
5757
};
5858
}
5959

60+
/// This function returns a validator that checks if the user [DateTime] input is
61+
/// in the past (before [DateTime.now]). If the checking results true, the validator
62+
/// returns `null`. Otherwise, it returns `isInThePastMsg`, if provided, or
63+
/// `FormBuilderLocalizations.current.dateMustBeInThePastErrorText`.
64+
Validator<DateTime> isInThePast({
65+
String? isInThePastMsg,
66+
}) {
67+
return (DateTime value) {
68+
return value.isBefore(DateTime.now())
69+
? null
70+
: isInThePastMsg ??
71+
FormBuilderLocalizations.current.dateMustBeInThePastErrorText;
72+
};
73+
}
74+
6075
String tmpIsDateTimeBetweenErrorMsg(DateTime left, DateTime right) {
6176
return 'The date must be after ${left.toLocal()} and before ${right.toLocal()}';
6277
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import 'package:flutter_test/flutter_test.dart';
2+
import 'package:form_builder_validators/localization/l10n.dart';
3+
import 'package:form_builder_validators/new_api_prototype/constants.dart';
4+
import 'package:form_builder_validators/new_api_prototype/datetime_validators.dart';
5+
6+
void main() {
7+
group('Validator: isInThePast', () {
8+
test('Validation for the current time', () {
9+
final DateTime after10Years =
10+
DateTime.now().add(const Duration(days: 365));
11+
final DateTime after60Seconds =
12+
DateTime.now().add(const Duration(seconds: 60));
13+
final DateTime before1Year =
14+
DateTime.now().subtract(const Duration(days: 365));
15+
final DateTime before60Seconds =
16+
DateTime.now().subtract(const Duration(seconds: 60));
17+
final Validator<DateTime> v = isInThePast();
18+
final String errorMsg =
19+
FormBuilderLocalizations.current.dateMustBeInThePastErrorText;
20+
21+
expect(
22+
v(after10Years),
23+
errorMsg,
24+
reason: 'Should return error against now + 10 years',
25+
);
26+
expect(
27+
v(after60Seconds),
28+
errorMsg,
29+
reason: 'Should return error against now + 60 seconds',
30+
);
31+
expect(
32+
v(before1Year),
33+
isNull,
34+
reason: 'Should return null against now - 1 year',
35+
);
36+
expect(
37+
v(before60Seconds),
38+
isNull,
39+
reason: 'Should return null against now - 60 seconds',
40+
);
41+
});
42+
43+
test('Should return a custom message after validating', () {
44+
const String errorMsg = 'error msg';
45+
final Validator<DateTime> v = isInThePast(isInThePastMsg: errorMsg);
46+
47+
expect(v(DateTime.now().add(const Duration(seconds: 1))), errorMsg);
48+
});
49+
});
50+
}

0 commit comments

Comments
 (0)