Skip to content

Commit 72ce35f

Browse files
committed
Fixes
1 parent 9684e3f commit 72ce35f

File tree

4 files changed

+59
-17
lines changed

4 files changed

+59
-17
lines changed

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ Available built-in type validators include:
8484
- `FormBuilderValidators.hasUppercaseChars()` - requires the field's to contain a specified number of uppercase characters.
8585
- `FormBuilderValidators.hasLowercaseChars()` - requires the field's to contain a specified number of lowercase characters.
8686
- `FormBuilderValidators.hasNumericChars()` - requires the field's to contain a specified number of numeric characters.
87-
- `FormBuilderValidators.conditional()` - requires the field's to validate with another validator conditionally.
8887
- `FormBuilderValidators.alphabetical()` - requires the field's to contain only alphabetical characters.
8988
- `FormBuilderValidators.oddNumber()` - requires the field's to be an odd number.
9089
- `FormBuilderValidators.evenNumber()` - requires the field's to be an even number.

lib/src/form_builder_validators.dart

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -462,17 +462,24 @@ class FormBuilderValidators {
462462
/// * [errorText] is the error message to display when the CVC is invalid
463463
static FormFieldValidator<String> creditCardCVC({
464464
String? errorText,
465-
}) =>
466-
compose<String>(
467-
[
468-
minLength(3,
469-
errorText: errorText ??
470-
FormBuilderLocalizations.current.creditCardCVCErrorText),
471-
maxLength(4,
472-
errorText: errorText ??
473-
FormBuilderLocalizations.current.creditCardCVCErrorText),
474-
],
475-
);
465+
}) {
466+
return (valueCandidate) {
467+
if (valueCandidate == null || valueCandidate.isEmpty) {
468+
return errorText ??
469+
FormBuilderLocalizations.current.creditCardCVCErrorText;
470+
} else {
471+
final cvc = int.tryParse(valueCandidate);
472+
if (cvc == null ||
473+
valueCandidate.length < 3 ||
474+
valueCandidate.length > 4) {
475+
return errorText ??
476+
FormBuilderLocalizations.current.creditCardCVCErrorText;
477+
} else {
478+
return null;
479+
}
480+
}
481+
};
482+
}
476483

477484
/// [FormFieldValidator] that requires the field's value to be a valid IP address.
478485
/// * [version] is a `String` or an `int`.
@@ -501,10 +508,11 @@ class FormBuilderValidators {
501508
static FormFieldValidator<String> time({
502509
String? errorText,
503510
}) =>
504-
(valueCandidate) =>
505-
valueCandidate?.isNotEmpty == true && !isDateTime(valueCandidate!)
506-
? errorText ?? FormBuilderLocalizations.current.timeErrorText
507-
: null;
511+
(valueCandidate) => valueCandidate == null ||
512+
valueCandidate.isEmpty ||
513+
!isTime(valueCandidate)
514+
? errorText ?? FormBuilderLocalizations.current.timeErrorText
515+
: null;
508516

509517
/// [FormFieldValidator] that requires the field's value to be a valid date.
510518
/// * [errorText] is the error message to display when the date is invalid

lib/src/utils/validators.dart

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ RegExp _macAddress = RegExp(r'^[0-9A-Fa-f]{2}$');
3535

3636
RegExp _bic = RegExp(r'^[A-Z]{4}[A-Z]{2}\w{2}(\w{3})?$');
3737

38+
RegExp _time = RegExp(
39+
r'^(?:[01]\d|2[0-3]):[0-5]\d$|^(?:0?[1-9]|1[0-2]):[0-5]\d\s?(?:[AaPp][Mm])$');
40+
3841
int _maxUrlLength = 2083;
3942

4043
/// check if the string [str] is an email
@@ -258,6 +261,10 @@ bool isDateTime(String str) {
258261
}
259262
}
260263

264+
bool isTime(String str) {
265+
return _time.hasMatch(str);
266+
}
267+
261268
/// check if the string is a valid phone number
262269
bool isPhoneNumber(String str) {
263270
if (str.isEmpty) {

test/form_builder_validators_test.dart

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,30 @@ void main() {
591591
}),
592592
);
593593

594+
testWidgets(
595+
'FormBuilderValidators.notMatch',
596+
(WidgetTester tester) => testValidations(tester, (context) {
597+
final validator = FormBuilderValidators.notMatch(r'^A[0-9]$');
598+
// Pass
599+
expect(validator('B1'), isNull);
600+
expect(validator('C2'), isNull);
601+
// Fail
602+
expect(validator('A1'), isNotNull);
603+
expect(validator('A9'), isNotNull);
604+
expect(validator(null), isNull);
605+
expect(validator(''), isNull);
606+
607+
final validatorWithErrorMessage = FormBuilderValidators.notMatch(
608+
r'^A[0-9]$',
609+
errorText: customErrorMessage,
610+
);
611+
// Pass
612+
expect(validatorWithErrorMessage('B1'), isNull);
613+
// Fail
614+
expect(validatorWithErrorMessage('A1'), customErrorMessage);
615+
}),
616+
);
617+
594618
testWidgets(
595619
'FormBuilderValidators.url',
596620
(WidgetTester tester) => testValidations(tester, (context) {
@@ -683,7 +707,11 @@ void main() {
683707
// Pass
684708
expect(validator('12:00'), isNull);
685709
expect(validator('23:59'), isNull);
710+
expect(validator('12:00 AM'), isNull);
711+
expect(validator('12:00 PM'), isNull);
712+
expect(validator('11:59 PM'), isNull);
686713
// Fail
714+
expect(validator('13:00 AM'), isNotNull);
687715
expect(validator('25:00'), isNotNull);
688716
expect(validator('invalid-time'), isNotNull);
689717
expect(validator(null), isNotNull);
@@ -833,7 +861,7 @@ void main() {
833861
// Pass
834862
expect(validatorWithErrorMessage('123'), isNull);
835863
// Fail
836-
expect(validator('12'), customErrorMessage);
864+
expect(validatorWithErrorMessage('12'), customErrorMessage);
837865
}),
838866
);
839867

0 commit comments

Comments
 (0)