Skip to content

Commit 6d50755

Browse files
committed
Add docs and tests
1 parent 9ab7253 commit 6d50755

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

lib/src/form_builder_validators.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -505,6 +505,8 @@ class FormBuilderValidators {
505505

506506
/// [FormFieldValidator] that requires the field's value to be a valid time string.
507507
/// * [errorText] is the error message to display when the time is invalid
508+
/// Valid time formats are `HH:mm:ss`, `HH:mm` and `HH:mm:ss.SSS` (24-hour clock)
509+
/// and `h:mm:ss a`, `h:mm a` and `h:mm:ss.SSS a` (12-hour clock)
508510
static FormFieldValidator<String> time({
509511
String? errorText,
510512
}) =>

lib/src/utils/validators.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ RegExp _bic = RegExp(r'^[A-Z]{4}[A-Z]{2}\w{2}(\w{3})?$');
158158
/// Examples: 23:59, 11:59 PM
159159
/// {@endtemplate}
160160
RegExp _time = RegExp(
161-
r'^(?:[01]\d|2[0-3]):[0-5]\d$|^(?:0?[1-9]|1[0-2]):[0-5]\d\s?(?:[AaPp][Mm])$');
161+
r'^(?:[01]?\d|2[0-3]):[0-5]?\d(?::[0-5]?\d)?$|^(?:0?[1-9]|1[0-2]):[0-5]?\d(?::[0-5]?\d)?\s?(?:[AaPp][Mm])$');
162162

163163
/// {@template upper_case_template}
164164
/// This regex matches any character that is not an uppercase letter (A-Z) or Ñ.

test/form_builder_validators_test.dart

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,11 +704,30 @@ void main() {
704704
(WidgetTester tester) => testValidations(tester, (context) {
705705
final validator = FormBuilderValidators.time();
706706
// Pass
707+
expect(validator('4:00'), isNull);
707708
expect(validator('12:00'), isNull);
708709
expect(validator('23:59'), isNull);
710+
711+
expect(validator('4:59:59'), isNull);
712+
expect(validator('4:4:59'), isNull);
713+
expect(validator('4:4:4'), isNull);
714+
expect(validator('11:59:59'), isNull);
715+
716+
expect(validator('04:04'), isNull);
717+
expect(validator('04:04:59'), isNull);
718+
expect(validator('04:04:04'), isNull);
719+
709720
expect(validator('12:00 AM'), isNull);
710721
expect(validator('12:00 PM'), isNull);
711722
expect(validator('11:59 PM'), isNull);
723+
724+
expect(validator('4:00:00 AM'), isNull);
725+
expect(validator('12:00:00 PM'), isNull);
726+
expect(validator('11:59:59 PM'), isNull);
727+
expect(validator('01:01:01 AM'), isNull);
728+
expect(validator('10:10:10 PM'), isNull);
729+
expect(validator('12:34:56 AM'), isNull);
730+
712731
// Fail
713732
expect(validator('13:00 AM'), isNotNull);
714733
expect(validator('25:00'), isNotNull);

0 commit comments

Comments
 (0)