Skip to content

Commit ce8dbaa

Browse files
committed
More
1 parent d1898d0 commit ce8dbaa

File tree

6 files changed

+77
-3
lines changed

6 files changed

+77
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
* Add date range
1515
* Add must be true
1616
* Add must be false
17+
* Add special characters
18+
* Add numeric characters
19+
* Add lowercase characters
20+
* Add uppercase characters
1721

1822
## 10.0.2
1923
* Reland generated l10n files

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ Available built-in validators include:
7777
- `FormBuilderValidators.fileSize()` - requires the field's to be less than the max size.
7878
- `FormBuilderValidators.mustBeTrue()` - requires the field's to be true.
7979
- `FormBuilderValidators.mustBeFalse()` - requires the field's to be false.
80-
80+
- `FormBuilderValidators.hasSpecialChars()` - requires the field's to contain a specified number of special characters.
81+
- `FormBuilderValidators.hasUppercaseChars()` - requires the field's to contain a specified number of uppercase characters.
82+
- `FormBuilderValidators.hasLowercaseChars()` - requires the field's to contain a specified number of lowercase characters.
83+
- `FormBuilderValidators.hasNumericChars()` - requires the field's to contain a specified number of numeric characters.
8184
### Supported languages
8285

8386
Validators support default `errorText` messages in these languages:

lib/l10n/intl_en.arb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,9 @@
2929
"fileSizeErrorText": "File size must be less than {maxSize} while it is {fileSize}",
3030
"dateRangeErrorText": "Date must be in range {min} - {max}",
3131
"mustBeTrueErrorText": "This field must be true.",
32-
"mustBeFalseErrorText": "This field must be false."
32+
"mustBeFalseErrorText": "This field must be false.",
33+
"containsSpecialCharErrorText": "Value must contain at least {min} special characters.",
34+
"containsUppercaseCharErrorText": "Value must contain at least {min} uppercase characters.",
35+
"containsLowercaseCharErrorText": "Value must contain at least {min} lowercase characters.",
36+
"containsNumberErrorText": "Value must contain at least {min} numbers."
3337
}

lib/src/form_builder_validators.dart

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,4 +512,51 @@ class FormBuilderValidators {
512512
(valueCandidate) => valueCandidate != false
513513
? errorText ?? FormBuilderLocalizations.current.mustBeFalseErrorText
514514
: null;
515+
516+
/// [FormFieldValidator] that requires the field's value to contain an amount of special characters.
517+
static FormFieldValidator<String> hasSpecialChars({
518+
int atLeast = 1,
519+
String? errorText,
520+
}) =>
521+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
522+
specialCharLength(valueCandidate!) >= atLeast
523+
? errorText ??
524+
FormBuilderLocalizations.current
525+
.containsSpecialCharErrorText(atLeast)
526+
: null;
527+
528+
/// [FormFieldValidator] that requires the field's value to contain an amount of uppercase characters.
529+
static FormFieldValidator<String> hasUppercaseChars({
530+
int atLeast = 1,
531+
String? errorText,
532+
}) =>
533+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
534+
uppercaseCharLength(valueCandidate!) >= atLeast
535+
? errorText ??
536+
FormBuilderLocalizations.current
537+
.containsUppercaseCharErrorText(atLeast)
538+
: null;
539+
540+
/// [FormFieldValidator] that requires the field's value to contain an amount of lowercase characters.
541+
static FormFieldValidator<String> hasLowercaseChars({
542+
int atLeast = 1,
543+
String? errorText,
544+
}) =>
545+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
546+
lowercaseCharLength(valueCandidate!) >= atLeast
547+
? errorText ??
548+
FormBuilderLocalizations.current
549+
.containsLowercaseCharErrorText(atLeast)
550+
: null;
551+
552+
/// [FormFieldValidator] that requires the field's value to contain an amount of numeric characters.
553+
static FormFieldValidator<String> hasNumericChars({
554+
int atLeast = 1,
555+
String? errorText,
556+
}) =>
557+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
558+
numberCharLength(valueCandidate!) >= atLeast
559+
? errorText ??
560+
FormBuilderLocalizations.current.containsNumberErrorText(atLeast)
561+
: null;
515562
}

lib/src/utils/helpers.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ String formatBytes(int bytes) {
3232
}
3333
// Truncate to 1 decimal place
3434
return '${size.toStringAsFixed(1)}${suffixes[i]}';
35-
}
35+
}

lib/src/utils/validators.dart

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,3 +328,19 @@ bool isColorCode(String value,
328328
}
329329
return false;
330330
}
331+
332+
int uppercaseCharLength(String value) {
333+
return value.replaceAll(RegExp(r'[^A-Z]'), '').length;
334+
}
335+
336+
int lowercaseCharLength(String value) {
337+
return value.replaceAll(RegExp(r'[^a-z]'), '').length;
338+
}
339+
340+
int numberCharLength(String value) {
341+
return value.replaceAll(RegExp(r'[^0-9]'), '').length;
342+
}
343+
344+
int specialCharLength(String value) {
345+
return value.replaceAll(RegExp(r'[A-Za-z0-9]'), '').length;
346+
}

0 commit comments

Comments
 (0)