Skip to content

Commit a428c37

Browse files
committed
More
1 parent b54330d commit a428c37

14 files changed

+356
-241
lines changed

lib/src/bool/has_lowercase_chars_validator.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import '../base_validator.dart';
44
class HasLowercaseCharsValidator extends BaseValidator<String> {
55
HasLowercaseCharsValidator({
66
this.atLeast = 1,
7+
8+
/// {@macro lower_case_template}
79
RegExp? regex,
810

911
/// {@macro base_validator_error_text}

lib/src/bool/has_numeric_chars_validator.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import '../base_validator.dart';
44
class HasNumericCharsValidator extends BaseValidator<String> {
55
HasNumericCharsValidator({
66
this.atLeast = 1,
7+
8+
/// {@macro numeric_chars_template}
79
RegExp? regex,
810

911
/// {@macro base_validator_error_text}
@@ -21,7 +23,7 @@ class HasNumericCharsValidator extends BaseValidator<String> {
2123
String get translatedErrorText =>
2224
FormBuilderLocalizations.current.containsNumberErrorText(atLeast);
2325

24-
/// {@template number_template}
26+
/// {@template numeric_chars_template}
2527
/// This regex matches any character that is not a digit (0-9).
2628
///
2729
/// - It includes special characters, letters, and other non-numeric characters.

lib/src/bool/has_special_chars_validator.dart

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import '../base_validator.dart';
44
class HasSpecialCharsValidator extends BaseValidator<String> {
55
HasSpecialCharsValidator({
66
this.atLeast = 1,
7+
8+
/// {@macro special_chars_template}
79
RegExp? regex,
810

911
/// {@macro base_validator_error_text}
@@ -21,7 +23,7 @@ class HasSpecialCharsValidator extends BaseValidator<String> {
2123
String get translatedErrorText =>
2224
FormBuilderLocalizations.current.containsSpecialCharErrorText(atLeast);
2325

24-
/// {@template special_char_template}
26+
/// {@template special_chars_template}
2527
/// This regex matches any character that is not a letter (A-Z, a-z) or a digit (0-9).
2628
///
2729
/// - It includes special characters and symbols.

lib/src/bool/has_uppercase_chars_validator.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import '../base_validator.dart';
44
class HasUppercaseCharsValidator extends BaseValidator<String> {
55
HasUppercaseCharsValidator({
66
this.atLeast = 1,
7+
8+
/// {@macro upper_case_template}
79
RegExp? regex,
810

911
/// {@macro base_validator_error_text}

lib/src/datetime/time_validator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import '../base_validator.dart';
33

44
class TimeValidator extends BaseValidator<String> {
55
TimeValidator({
6+
/// {@macro time_template}
67
RegExp? regex,
78

89
/// {@macro base_validator_error_text}

lib/src/file/path_validator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import '../base_validator.dart';
33

44
class PathValidator extends BaseValidator<String?> {
55
PathValidator({
6+
/// {@macro file_path_template}
67
RegExp? regex,
78

89
/// {@macro base_validator_error_text}

lib/src/form_builder_validators.dart

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -331,21 +331,21 @@ class FormBuilderValidators {
331331
bool allowUnderscore = false,
332332
List<String> hostWhitelist = const <String>[],
333333
List<String> hostBlacklist = const <String>[],
334+
RegExp? regex,
334335
String? errorText,
335336
bool checkNullOrEmpty = true,
336337
}) =>
337-
(String? valueCandidate) => valueCandidate?.isNotEmpty == true &&
338-
!isURL(
339-
valueCandidate,
340-
protocols: protocols,
341-
requireTld: requireTld,
342-
requireProtocol: requireProtocol,
343-
allowUnderscore: allowUnderscore,
344-
hostWhitelist: hostWhitelist,
345-
hostBlacklist: hostBlacklist,
346-
)
347-
? errorText ?? FormBuilderLocalizations.current.urlErrorText
348-
: null;
338+
UrlValidator(
339+
protocols: protocols,
340+
requireTld: requireTld,
341+
requireProtocol: requireProtocol,
342+
allowUnderscore: allowUnderscore,
343+
hostWhitelist: hostWhitelist,
344+
hostBlacklist: hostBlacklist,
345+
regex: regex,
346+
errorText: errorText,
347+
checkNullOrEmpty: checkNullOrEmpty,
348+
).validate;
349349

350350
/// [FormFieldValidator] that requires the field's value to match the provided regex pattern.
351351
/// This validator checks if the field's value matches the given regex pattern.
@@ -611,17 +611,15 @@ class FormBuilderValidators {
611611
///
612612
/// {@macro phone_number_template}
613613
static FormFieldValidator<String> phoneNumber({
614+
RegExp? regex,
614615
String? errorText,
615616
bool checkNullOrEmpty = true,
616617
}) =>
617-
(String? valueCandidate) {
618-
if (valueCandidate == null || valueCandidate.isEmpty) {
619-
return errorText ?? FormBuilderLocalizations.current.phoneErrorText;
620-
}
621-
return !isPhoneNumber(valueCandidate)
622-
? errorText ?? FormBuilderLocalizations.current.phoneErrorText
623-
: null;
624-
};
618+
PhoneNumberValidator(
619+
regex: regex,
620+
errorText: errorText,
621+
checkNullOrEmpty: checkNullOrEmpty,
622+
).validate;
625623

626624
/// [FormFieldValidator] that requires the field's value to be a valid color code.
627625
/// This validator checks if the field's value is a valid color code.
@@ -1155,13 +1153,15 @@ class FormBuilderValidators {
11551153
///
11561154
/// {@macro mac_address_template}
11571155
static FormFieldValidator<String> macAddress({
1156+
RegExp? regex,
11581157
String? errorText,
11591158
bool checkNullOrEmpty = true,
11601159
}) =>
1161-
(String? valueCandidate) => valueCandidate?.isEmpty != false ||
1162-
!isMACAddress(valueCandidate!)
1163-
? errorText ?? FormBuilderLocalizations.current.macAddressErrorText
1164-
: null;
1160+
MacAddressValidator(
1161+
regex: regex,
1162+
errorText: errorText,
1163+
checkNullOrEmpty: checkNullOrEmpty,
1164+
).validate;
11651165

11661166
/// [FormFieldValidator] that requires the field's value to start with a specific value.
11671167
/// This validator checks if the field's value starts with the given prefix.

lib/src/network/email_validator.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import '../base_validator.dart';
1414
/// {@endtemplate}
1515
class EmailValidator extends BaseValidator<String> {
1616
EmailValidator({
17+
/// {@macro email_regex_template}
1718
RegExp? regex,
1819

1920
/// {@macro base_validator_error_text}

lib/src/network/ip_validator.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import '../base_validator.dart';
44
class IpValidator extends BaseValidator<String?> {
55
IpValidator({
66
int this.version = 4,
7+
8+
/// {@macro ipv4_template}
9+
/// {@macro ipv6_template}
710
RegExp? this.regex,
811

912
/// {@macro base_validator_error_text}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,53 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class MacAddressValidator extends BaseValidator<String?> {
5+
MacAddressValidator({
6+
/// {@macro mac_address_template}
7+
RegExp? regex,
8+
9+
/// {@macro base_validator_error_text}
10+
super.errorText,
11+
12+
/// {@macro base_validator_null_check}
13+
super.checkNullOrEmpty,
14+
}) : regex = regex ?? _macAddress;
15+
16+
final RegExp regex;
17+
18+
/// {@template mac_address_template}
19+
/// This regex matches MAC addresses.
20+
///
21+
/// - It consists of six groups of two hexadecimal digits.
22+
/// - Each group is separated by a colon.
23+
///
24+
/// Examples: 00:1A:2B:3C:4D:5E, 00:1A:2B:3C:4D:5E
25+
/// {@endtemplate}
26+
static final RegExp _macAddress =
27+
RegExp(r'^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$');
28+
29+
@override
30+
String get translatedErrorText =>
31+
FormBuilderLocalizations.current.macAddressErrorText;
32+
33+
@override
34+
String? validateValue(String? valueCandidate) {
35+
return !isMACAddress(valueCandidate!) ? errorText : null;
36+
}
37+
38+
/// check if the string is a valid MAC address
39+
bool isMACAddress(String value) {
40+
final String splitChar = value.contains(':') ? ':' : '-';
41+
final List<String> parts = value.split(splitChar);
42+
if (parts.length != 6) {
43+
return false;
44+
}
45+
46+
for (final String part in parts) {
47+
if (part.length != 2 || !regex.hasMatch(part)) {
48+
return false;
49+
}
50+
}
51+
return true;
52+
}
53+
}

0 commit comments

Comments
 (0)