Skip to content

Commit 0154995

Browse files
committed
More
1 parent 9ae9349 commit 0154995

File tree

6 files changed

+88
-25
lines changed

6 files changed

+88
-25
lines changed

lib/src/form_builder_validators.dart

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -382,26 +382,28 @@ class FormBuilderValidators {
382382
String? errorText,
383383
bool checkNullOrEmpty = true,
384384
}) =>
385-
(String? valueCandidate) =>
386-
valueCandidate?.isNotEmpty == true && !regex.hasMatch(valueCandidate!)
387-
? errorText ?? FormBuilderLocalizations.current.matchErrorText
388-
: null;
385+
MatchValidator(
386+
regex,
387+
errorText: errorText,
388+
checkNullOrEmpty: checkNullOrEmpty,
389+
).validate;
389390

390391
/// [FormFieldValidator] that requires the field's value not to match the provided regex pattern.
391392
/// This validator checks if the field's value does not match the given regex pattern.
392393
///
393394
/// ## Parameters:
394395
/// - [regex] The regex pattern to match.
395396
/// - [errorText] The error message to display when the value matches the pattern.
396-
static FormFieldValidator<String> notMatch(
397+
static FormFieldValidator<String> matchNot(
397398
RegExp regex, {
398399
String? errorText,
399400
bool checkNullOrEmpty = true,
400401
}) =>
401-
(String? valueCandidate) =>
402-
valueCandidate?.isNotEmpty == true && regex.hasMatch(valueCandidate!)
403-
? errorText ?? FormBuilderLocalizations.current.matchErrorText
404-
: null;
402+
MatchNotValidator(
403+
regex,
404+
errorText: errorText,
405+
checkNullOrEmpty: checkNullOrEmpty,
406+
).validate;
405407

406408
/// [FormFieldValidator] that requires the field's value to be a valid number.
407409
/// This validator checks if the field's value is a valid number.
@@ -915,32 +917,32 @@ class FormBuilderValidators {
915917
FormBuilderValidators.minLength(minLength, errorText: errorText),
916918
FormBuilderValidators.maxLength(maxLength, errorText: errorText),
917919
if (!allowNumbers)
918-
FormBuilderValidators.notMatch(
920+
FormBuilderValidators.matchNot(
919921
RegExp('[0-9]'),
920922
errorText: errorText,
921923
),
922924
if (!allowUnderscore)
923-
FormBuilderValidators.notMatch(
925+
FormBuilderValidators.matchNot(
924926
RegExp('_'),
925927
errorText: errorText,
926928
),
927929
if (!allowDots)
928-
FormBuilderValidators.notMatch(
930+
FormBuilderValidators.matchNot(
929931
RegExp(r'\.'),
930932
errorText: errorText,
931933
),
932934
if (!allowDash)
933-
FormBuilderValidators.notMatch(
935+
FormBuilderValidators.matchNot(
934936
RegExp('-'),
935937
errorText: errorText,
936938
),
937939
if (!allowSpace)
938-
FormBuilderValidators.notMatch(
940+
FormBuilderValidators.matchNot(
939941
RegExp(r'\s'),
940942
errorText: errorText,
941943
),
942944
if (!allowSpecialChar)
943-
FormBuilderValidators.notMatch(
945+
FormBuilderValidators.matchNot(
944946
RegExp(r'[!@#\$%^&*(),.?":{}|<>]'),
945947
errorText: errorText,
946948
),
@@ -1314,8 +1316,8 @@ class FormBuilderValidators {
13141316
String? errorText,
13151317
bool checkNullOrEmpty = true,
13161318
}) =>
1317-
(String? valueCandidate) => valueCandidate?.isEmpty != false ||
1318-
!isSingleLine(valueCandidate!)
1319-
? errorText ?? FormBuilderLocalizations.current.singleLineErrorText
1320-
: null;
1319+
SingleLineValidator(
1320+
errorText: errorText,
1321+
checkNullOrEmpty: checkNullOrEmpty,
1322+
).validate;
13211323
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class MatchNotValidator extends BaseValidator<String> {
5+
const MatchNotValidator(
6+
this.regex, {
7+
/// {@macro base_validator_error_text}
8+
super.errorText,
9+
10+
/// {@macro base_validator_null_check}
11+
super.checkNullOrEmpty,
12+
});
13+
14+
final RegExp regex;
15+
16+
@override
17+
String get translatedErrorText =>
18+
FormBuilderLocalizations.current.matchErrorText;
19+
20+
@override
21+
String? validateValue(String? valueCandidate) {
22+
return regex.hasMatch(valueCandidate!) ? null : errorText;
23+
}
24+
}

lib/src/string/match_validator.dart

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class MatchValidator extends BaseValidator<String> {
5+
const MatchValidator(
6+
this.regex, {
7+
/// {@macro base_validator_error_text}
8+
super.errorText,
9+
10+
/// {@macro base_validator_null_check}
11+
super.checkNullOrEmpty,
12+
});
13+
14+
final RegExp regex;
15+
16+
@override
17+
String get translatedErrorText =>
18+
FormBuilderLocalizations.current.matchErrorText;
19+
20+
@override
21+
String? validateValue(String? valueCandidate) {
22+
return !regex.hasMatch(valueCandidate!) ? null : errorText;
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class SingleLineValidator extends BaseValidator<String> {
5+
const SingleLineValidator({
6+
/// {@macro base_validator_error_text}
7+
super.errorText,
8+
9+
/// {@macro base_validator_null_check}
10+
super.checkNullOrEmpty,
11+
});
12+
13+
@override
14+
String get translatedErrorText =>
15+
FormBuilderLocalizations.current.singleLineErrorText;
16+
17+
@override
18+
String? validateValue(String? valueCandidate) {
19+
return !valueCandidate!.contains('\n') && !valueCandidate.contains('\r')
20+
? errorText
21+
: null;
22+
}
23+
}

lib/src/utils/validators.dart

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,3 @@ bool isBIC(String bic) {
285285

286286
return _bic.hasMatch(bic);
287287
}
288-
289-
bool isSingleLine(String value) {
290-
return !value.contains('\n') && !value.contains('\r');
291-
}

test/src/form_builder_validators_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@ void main() {
685685
'FormBuilderValidators.notMatch',
686686
(WidgetTester tester) => testValidations(tester, (BuildContext context) {
687687
final FormFieldValidator<String> validator =
688-
FormBuilderValidators.notMatch(RegExp(r'^A[0-9]$'));
688+
FormBuilderValidators.matchNot(RegExp(r'^A[0-9]$'));
689689
// Pass
690690
expect(validator('B1'), isNull);
691691
expect(validator('C2'), isNull);
@@ -696,7 +696,7 @@ void main() {
696696
expect(validator(''), isNull);
697697

698698
final FormFieldValidator<String> validatorWithErrorMessage =
699-
FormBuilderValidators.notMatch(
699+
FormBuilderValidators.matchNot(
700700
RegExp(r'^A[0-9]$'),
701701
errorText: customErrorMessage,
702702
);

0 commit comments

Comments
 (0)