Skip to content

Commit 9ae9349

Browse files
committed
More
1 parent 3d2c837 commit 9ae9349

8 files changed

+177
-45
lines changed

lib/src/form_builder_validators.dart

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -679,10 +679,10 @@ class FormBuilderValidators {
679679
String? errorText,
680680
bool checkNullOrEmpty = true,
681681
}) =>
682-
(String? valueCandidate) => valueCandidate?.isNotEmpty == true &&
683-
valueCandidate!.toUpperCase() != valueCandidate
684-
? errorText ?? FormBuilderLocalizations.current.uppercaseErrorText
685-
: null;
682+
UppercaseValidator(
683+
errorText: errorText,
684+
checkNullOrEmpty: checkNullOrEmpty,
685+
).validate;
686686

687687
/// [FormFieldValidator] that requires the field's value to be lowercase.
688688
/// This validator checks if the field's value is lowercase.
@@ -693,10 +693,10 @@ class FormBuilderValidators {
693693
String? errorText,
694694
bool checkNullOrEmpty = true,
695695
}) =>
696-
(String? valueCandidate) => valueCandidate?.isNotEmpty == true &&
697-
valueCandidate!.toLowerCase() != valueCandidate
698-
? errorText ?? FormBuilderLocalizations.current.lowercaseErrorText
699-
: null;
696+
LowercaseValidator(
697+
errorText: errorText,
698+
checkNullOrEmpty: checkNullOrEmpty,
699+
).validate;
700700

701701
/// [FormFieldValidator] that requires the field's value to be a valid file extension.
702702
/// This validator checks if the field's value is a valid file extension.
@@ -1005,14 +1005,15 @@ class FormBuilderValidators {
10051005
///
10061006
/// {@macro alphabetical_template}
10071007
static FormFieldValidator<String> alphabetical({
1008+
RegExp? regex,
10081009
String? errorText,
10091010
bool checkNullOrEmpty = true,
10101011
}) =>
1011-
(String? valueCandidate) => valueCandidate == null ||
1012-
valueCandidate.isEmpty ||
1013-
!isAlphabetical(valueCandidate)
1014-
? errorText ?? FormBuilderLocalizations.current.alphabeticalErrorText
1015-
: null;
1012+
AlphabeticalValidator(
1013+
regex: regex,
1014+
errorText: errorText,
1015+
checkNullOrEmpty: checkNullOrEmpty,
1016+
).validate;
10161017

10171018
/// [FormFieldValidator] that requires the field's value to be a valid UUID.
10181019
/// This validator checks if the field's value is a valid UUID.
@@ -1180,11 +1181,11 @@ class FormBuilderValidators {
11801181
String? errorText,
11811182
bool checkNullOrEmpty = true,
11821183
}) =>
1183-
(String? valueCandidate) => valueCandidate?.isEmpty != false ||
1184-
!valueCandidate!.startsWith(prefix)
1185-
? errorText ??
1186-
FormBuilderLocalizations.current.startsWithErrorText(prefix)
1187-
: null;
1184+
StartsWithValidator(
1185+
prefix,
1186+
errorText: errorText,
1187+
checkNullOrEmpty: checkNullOrEmpty,
1188+
).validate;
11881189

11891190
/// [FormFieldValidator] that requires the field's value to end with a specific value.
11901191
/// This validator checks if the field's value ends with the given suffix.
@@ -1197,11 +1198,11 @@ class FormBuilderValidators {
11971198
String? errorText,
11981199
bool checkNullOrEmpty = true,
11991200
}) =>
1200-
(String? valueCandidate) =>
1201-
valueCandidate?.isEmpty != false || !valueCandidate!.endsWith(suffix)
1202-
? errorText ??
1203-
FormBuilderLocalizations.current.endsWithErrorText(suffix)
1204-
: null;
1201+
EndsWithValidator(
1202+
suffix,
1203+
errorText: errorText,
1204+
checkNullOrEmpty: checkNullOrEmpty,
1205+
).validate;
12051206

12061207
/// [FormFieldValidator] that requires the field's value to contain a specific value.
12071208
/// This validator checks if the field's value contains the given substring.
@@ -1216,15 +1217,12 @@ class FormBuilderValidators {
12161217
String? errorText,
12171218
bool checkNullOrEmpty = true,
12181219
}) =>
1219-
(String? valueCandidate) => valueCandidate?.isEmpty != false ||
1220-
caseSensitive && !valueCandidate!.contains(substring) ||
1221-
!caseSensitive &&
1222-
!valueCandidate!
1223-
.toLowerCase()
1224-
.contains(substring.toLowerCase())
1225-
? errorText ??
1226-
FormBuilderLocalizations.current.containsErrorText(substring)
1227-
: null;
1220+
ContainsValidator(
1221+
substring,
1222+
caseSensitive: caseSensitive,
1223+
errorText: errorText,
1224+
checkNullOrEmpty: checkNullOrEmpty,
1225+
).validate;
12281226

12291227
/// [FormFieldValidator] that requires the field's value to be between two numbers.
12301228
/// This validator checks if the field's value is between the given minimum and maximum values.
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,35 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class AlphabeticalValidator extends BaseValidator<String> {
5+
AlphabeticalValidator({
6+
/// {@macro alphabetical_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 ?? _alphabetical;
15+
16+
final RegExp regex;
17+
18+
/// {@template alphabetical_template}
19+
/// This regex matches only alphabetical characters.
20+
///
21+
/// - It allows both uppercase and lowercase letters.
22+
///
23+
/// Examples: abcdef, XYZ
24+
/// {@endtemplate}
25+
static final RegExp _alphabetical = RegExp(r'^[a-zA-Z]+$');
26+
27+
@override
28+
String get translatedErrorText =>
29+
FormBuilderLocalizations.current.alphabeticalErrorText;
30+
31+
@override
32+
String? validateValue(String? valueCandidate) {
33+
return regex.hasMatch(valueCandidate!) ? null : errorText;
34+
}
35+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class ContainsValidator extends BaseValidator<String> {
5+
const ContainsValidator(
6+
this.substring, {
7+
this.caseSensitive = true,
8+
9+
/// {@macro base_validator_error_text}
10+
super.errorText,
11+
12+
/// {@macro base_validator_null_check}
13+
super.checkNullOrEmpty,
14+
});
15+
16+
final String substring;
17+
18+
final bool caseSensitive;
19+
20+
@override
21+
String get translatedErrorText =>
22+
FormBuilderLocalizations.current.containsErrorText(substring);
23+
24+
@override
25+
String? validateValue(String? valueCandidate) {
26+
if (caseSensitive) {
27+
return valueCandidate!.contains(substring) ? null : errorText;
28+
} else {
29+
return valueCandidate!.toLowerCase().contains(substring.toLowerCase())
30+
? null
31+
: errorText;
32+
}
33+
}
34+
}
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 EndsWithValidator extends BaseValidator<String> {
5+
const EndsWithValidator(
6+
this.suffix, {
7+
/// {@macro base_validator_error_text}
8+
super.errorText,
9+
10+
/// {@macro base_validator_null_check}
11+
super.checkNullOrEmpty,
12+
});
13+
14+
final String suffix;
15+
16+
@override
17+
String get translatedErrorText =>
18+
FormBuilderLocalizations.current.endsWithErrorText(suffix);
19+
20+
@override
21+
String? validateValue(String? valueCandidate) {
22+
return valueCandidate!.endsWith(suffix) ? null : errorText;
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class LowercaseValidator extends BaseValidator<String> {
5+
const LowercaseValidator({
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.lowercaseErrorText;
16+
17+
@override
18+
String? validateValue(String? valueCandidate) {
19+
return valueCandidate!.toLowerCase() == valueCandidate ? null : errorText;
20+
}
21+
}
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 StartsWithValidator extends BaseValidator<String> {
5+
const StartsWithValidator(
6+
this.prefix, {
7+
/// {@macro base_validator_error_text}
8+
super.errorText,
9+
10+
/// {@macro base_validator_null_check}
11+
super.checkNullOrEmpty,
12+
});
13+
14+
final String prefix;
15+
16+
@override
17+
String get translatedErrorText =>
18+
FormBuilderLocalizations.current.startsWithErrorText(prefix);
19+
20+
@override
21+
String? validateValue(String? valueCandidate) {
22+
return valueCandidate!.startsWith(prefix) ? null : errorText;
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,21 @@
11
import '../../localization/l10n.dart';
22
import '../base_validator.dart';
3+
4+
class UppercaseValidator extends BaseValidator<String> {
5+
const UppercaseValidator({
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.uppercaseErrorText;
16+
17+
@override
18+
String? validateValue(String? valueCandidate) {
19+
return valueCandidate!.toUpperCase() == valueCandidate ? null : errorText;
20+
}
21+
}

lib/src/utils/validators.dart

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,6 @@ RegExp _rgb = RegExp(r'^rgb\(\d{1,3},\s*\d{1,3},\s*\d{1,3}\)$');
6565
/// {@endtemplate}
6666
RegExp _hsl = RegExp(r'^hsl\(\d+,\s*\d+%,\s*\d+%\)$');
6767

68-
/// {@template alphabetical_template}
69-
/// This regex matches only alphabetical characters.
70-
///
71-
/// - It allows both uppercase and lowercase letters.
72-
///
73-
/// Examples: abcdef, XYZ
74-
/// {@endtemplate}
75-
RegExp _alphabetical = RegExp(r'^[a-zA-Z]+$');
76-
7768
/// {@template uuid_template}
7869
/// This regex matches UUIDs (version 4).
7970
///
@@ -208,10 +199,6 @@ bool isColorCode(
208199
return false;
209200
}
210201

211-
bool isAlphabetical(String value) {
212-
return _alphabetical.hasMatch(value);
213-
}
214-
215202
/// check if the string is a valid UUID
216203
bool isUUID(String value) {
217204
return _uuid.hasMatch(value);

0 commit comments

Comments
 (0)