Skip to content

Commit 5f7c10c

Browse files
committed
Add password
1 parent 608fa12 commit 5f7c10c

File tree

4 files changed

+49
-1
lines changed

4 files changed

+49
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
* Add numeric characters
1919
* Add lowercase characters
2020
* Add uppercase characters
21+
* Add password
2122

2223
## 10.0.2
2324
* Reland generated l10n files

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ Available built-in validators include:
8282
- `FormBuilderValidators.hasLowercaseChars()` - requires the field's to contain a specified number of lowercase characters.
8383
- `FormBuilderValidators.hasNumericChars()` - requires the field's to contain a specified number of numeric characters.
8484
- `FormBuilderValidators.conditional()` - requires the field's to validate with another validator conditionally.
85+
- `FormBuilderValidators.conditional()` - requires the field's to be a valid password that matched required conditions.
8586

8687
### Supported languages
8788

lib/src/form_builder_validators.dart

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ class FormBuilderValidators {
323323
String? errorText,
324324
}) =>
325325
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
326-
!isCreditCardExpirationDate(valueCandidate!) ||
326+
!isCreditCardExpirationDate(valueCandidate!) ||
327327
!isNotExpiredCreditCardDate(valueCandidate!)
328328
? errorText ??
329329
FormBuilderLocalizations.current.creditCardExpiredErrorText
@@ -559,4 +559,37 @@ class FormBuilderValidators {
559559
? null
560560
: errorText ??
561561
FormBuilderLocalizations.current.containsNumberErrorText(atLeast);
562+
563+
/// [FormFieldValidator] that requires the field's value to be a valid password.
564+
/// * [minLength] is the minimum length of the password. By default `8`
565+
/// * [maxLength] is the maximum length of the password. By default `32`
566+
/// * [uppercase] is the minimum amount of uppercase characters. By default `1`
567+
/// * [lowercase] is the minimum amount of lowercase characters. By default `1`
568+
/// * [number] is the minimum amount of numeric characters. By default `1`
569+
/// * [specialChar] is the minimum amount of special characters. By default `1`
570+
/// * [errorText] is the error message to display when the password is invalid
571+
static FormFieldValidator<String> password({
572+
int minLength = 8,
573+
int maxLength = 32,
574+
int uppercase = 1,
575+
int lowercase = 1,
576+
int number = 1,
577+
int specialChar = 1,
578+
String? errorText,
579+
}) {
580+
return FormBuilderValidators.compose<String>(
581+
[
582+
FormBuilderValidators.minLength(minLength, errorText: errorText),
583+
FormBuilderValidators.maxLength(maxLength, errorText: errorText),
584+
FormBuilderValidators.hasUppercaseChars(
585+
atLeast: uppercase, errorText: errorText),
586+
FormBuilderValidators.hasLowercaseChars(
587+
atLeast: lowercase, errorText: errorText),
588+
FormBuilderValidators.hasNumericChars(
589+
atLeast: number, errorText: errorText),
590+
FormBuilderValidators.hasSpecialChars(
591+
atLeast: specialChar, errorText: errorText),
592+
],
593+
);
594+
}
562595
}

test/form_builder_validators_test.dart

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,4 +713,17 @@ void main() {
713713
expect(validator('hello'), isNotNull);
714714
}),
715715
);
716+
717+
testWidgets(
718+
'FormBuilderValidators.password',
719+
(WidgetTester tester) => testValidations(tester, (context) {
720+
final validator = FormBuilderValidators.password();
721+
// Pass
722+
expect(validator('Hellohello1@'), isNull);
723+
// Fail
724+
expect(validator('hellohello1@'), isNotNull);
725+
expect(validator('Hellohello1'), isNotNull);
726+
expect(validator('Hellohello@'), isNotNull);
727+
}),
728+
);
716729
}

0 commit comments

Comments
 (0)