Skip to content

Commit 77b67de

Browse files
committed
More
1 parent 927a761 commit 77b67de

File tree

6 files changed

+516
-15
lines changed

6 files changed

+516
-15
lines changed

CHANGELOG.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## 10.1.0
22

3+
* Re-land generated l10n files
4+
* Add Bulgarian
35
* Add creditCardExpirationDate
46
* Add creditCardCVC
57
* Add colorCode
@@ -18,8 +20,20 @@
1820
* Add lowercase characters
1921
* Add uppercase characters
2022
* Add password
21-
* Re-land generated l10n files
22-
* Add Bulgarian
23+
* Add alphabetical
24+
* Add uuid
25+
* Add json
26+
* Add latitude
27+
* Add longitude
28+
* Add base64
29+
* Add path
30+
* Add port number
31+
* Add mac address
32+
* Add startWith
33+
* Add endWith
34+
* Add contains
35+
* Add between
36+
* Add inList
2337

2438
## 10.0.1
2539
* Fix regression (include l10n files)

README.md

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,17 @@ Also included is the `l10n` / `i18n` of error text messages to multiple language
4646
This package comes with several most common `FormFieldValidator`s such as required, numeric, mail,
4747
URL, min, max, minLength, maxLength, minWordsCount, maxWordsCount, IP, credit card, etc., with default `errorText` messages.
4848

49-
Available built-in validators include:
49+
Available built-in helper validators:
50+
51+
- `FormBuilderValidators.compose()` - runs each validator against the value provided.
52+
- `FormBuilderValidators.conditional()` - conditionally runs a validator against the value provided.
53+
54+
Available built-in type validators include:
5055

51-
- `FormBuilderValidators.creditCard()` - requires the field's value to be a valid credit card number.
52-
- `FormBuilderValidators.creditCardExpirationDate()` - requires the field's value to be a valid credit card expiration date and can check if not expired yet.
53-
- `FormBuilderValidators.creditCardCVC()` - requires the field's value to be a valid credit card CVC number.
54-
- `FormBuilderValidators.colorCode()` - requires the field's value to be a valid color code.
55-
- `FormBuilderValidators.date()` - requires the field's value to be a valid date string.
56-
- `FormBuilderValidators.dateRange()` - requires the field's value to be a within a date range.
57-
- `FormBuilderValidators.email()` - requires the field's value to be a valid email address.
5856
- `FormBuilderValidators.equal()` - requires the field's value to be equal to the provided object.
5957
- `FormBuilderValidators.integer()` - requires the field's value to be an integer.
60-
- `FormBuilderValidators.ip()` - requires the field's value to be a valid IP address.
58+
- `FormBuilderValidators.date()` - requires the field's value to be a valid date string.
59+
- `FormBuilderValidators.dateRange()` - requires the field's value to be a within a date range.
6160
- `FormBuilderValidators.match()` - requires the field's value to match the provided regex pattern.
6261
- `FormBuilderValidators.notMatch()` - requires the field's value to not match the provided regex pattern.
6362
- `FormBuilderValidators.max()` - requires the field's value to be less than or equal to the provided number.
@@ -69,19 +68,41 @@ Available built-in validators include:
6968
- `FormBuilderValidators.equalLength()` - requires the length of the field's value to be equal to the provided minimum length.
7069
- `FormBuilderValidators.numeric()` - requires the field's value to be a valid number.
7170
- `FormBuilderValidators.required()` - requires the field to have a non-empty value.
72-
- `FormBuilderValidators.url()` - requires the field's value to be a valid URL.
7371
- `FormBuilderValidators.uppercase()` - requires the field's value to be uppercase.
7472
- `FormBuilderValidators.lowercase()` - requires the field's value to be lowercase.
75-
- `FormBuilderValidators.fileExtension()` - requires the field's value to a valid file extension.
76-
- `FormBuilderValidators.fileSize()` - requires the field's to be less than the max size.
7773
- `FormBuilderValidators.mustBeTrue()` - requires the field's to be true.
7874
- `FormBuilderValidators.mustBeFalse()` - requires the field's to be false.
7975
- `FormBuilderValidators.hasSpecialChars()` - requires the field's to contain a specified number of special characters.
8076
- `FormBuilderValidators.hasUppercaseChars()` - requires the field's to contain a specified number of uppercase characters.
8177
- `FormBuilderValidators.hasLowercaseChars()` - requires the field's to contain a specified number of lowercase characters.
8278
- `FormBuilderValidators.hasNumericChars()` - requires the field's to contain a specified number of numeric characters.
8379
- `FormBuilderValidators.conditional()` - requires the field's to validate with another validator conditionally.
80+
- `FormBuilderValidators.alphabetical()` - requires the field's to contain only alphabetical characters.
81+
- `FormBuilderValidators.oddNumber()` - requires the field's to be an odd number.
82+
- `FormBuilderValidators.evenNumber()` - requires the field's to be an even number.
83+
- `FormBuilderValidators.between()` - requires the field's to be between two numbers.
84+
- `FormBuilderValidators.inList()` - requires the field's to be in the provided list.
85+
86+
Available built-in use-case validators include:
87+
88+
- `FormBuilderValidators.creditCard()` - requires the field's value to be a valid credit card number.
89+
- `FormBuilderValidators.creditCardExpirationDate()` - requires the field's value to be a valid credit card expiration date and can check if not expired yet.
90+
- `FormBuilderValidators.creditCardCVC()` - requires the field's value to be a valid credit card CVC number.
91+
- `FormBuilderValidators.colorCode()` - requires the field's value to be a valid color code.
92+
- `FormBuilderValidators.email()` - requires the field's value to be a valid email address.
93+
- `FormBuilderValidators.ip()` - requires the field's value to be a valid IP address.
94+
- `FormBuilderValidators.url()` - requires the field's value to be a valid URL.
95+
- `FormBuilderValidators.fileExtension()` - requires the field's value to a valid file extension.
96+
- `FormBuilderValidators.fileSize()` - requires the field's to be less than the max size.
8497
- `FormBuilderValidators.password()` - requires the field's to be a valid password that matched required conditions.
98+
- `FormBuilderValidators.uuid()` - requires the field's to be a valid uuid.
99+
- `FormBuilderValidators.json()` - requires the field's to be a valid json string.
100+
- `FormBuilderValidators.latitude()` - requires the field's to be a valid latitude.
101+
- `FormBuilderValidators.longitude()` - requires the field's to be a valid longitude.
102+
- `FormBuilderValidators.base64()` - requires the field's to be a valid base64 string.
103+
- `FormBuilderValidators.path()` - requires the field's to be a valid file or folder path.
104+
- `FormBuilderValidators.portNumber()` - requires the field's to be an valid port number.
105+
- `FormBuilderValidators.macAddress()` - requires the field's to be an valid MAC address.
85106

86107
### Supported languages
87108

lib/l10n/intl_en.arb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,21 @@
3333
"containsSpecialCharErrorText": "Value must contain at least {min} special characters.",
3434
"containsUppercaseCharErrorText": "Value must contain at least {min} uppercase characters.",
3535
"containsLowercaseCharErrorText": "Value must contain at least {min} lowercase characters.",
36-
"containsNumberErrorText": "Value must contain at least {min} numbers."
36+
"containsNumberErrorText": "Value must contain at least {min} numbers.",
37+
"alphabeticalErrorText": "Value must be alphabetical.",
38+
"uuidErrorText": "Value must be a valid UUID.",
39+
"jsonErrorText": "Value must be valid JSON.",
40+
"latitudeErrorText": "Value must be a valid latitude.",
41+
"longitudeErrorText": "Value must be a valid longitude.",
42+
"base64ErrorText": "Value must be a valid base64 string.",
43+
"pathErrorText": "Value must be a valid path.",
44+
"oddNumberErrorText": "Value must be an odd number.",
45+
"evenNumberErrorText": "Value must be an even number.",
46+
"portNumberErrorText": "Value must be a valid port number between {min} and {max}.",
47+
"macAddressErrorText": "Value must be a valid MAC address.",
48+
"startsWithErrorText": "Value must start with {value}.",
49+
"endsWithErrorText": "Value must end with {value}.",
50+
"containsErrorText": "Value must contain {value}.",
51+
"betweenErrorText": "Value must be between {min} and {max}.",
52+
"valueInListErrorText": "Value must be in list."
3753
}

lib/src/form_builder_validators.dart

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -567,4 +567,175 @@ class FormBuilderValidators {
567567
],
568568
);
569569
}
570+
571+
/// [FormFieldValidator] that requires the field's value to be alphabetical.
572+
static FormFieldValidator<String> alphabetical({
573+
String? errorText,
574+
}) =>
575+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
576+
!isAlphabetical(valueCandidate!)
577+
? errorText ?? FormBuilderLocalizations.current.alphabeticalErrorText
578+
: null;
579+
580+
/// [FormFieldValidator] that requires the field's value to be a valid uuid.
581+
static FormFieldValidator<String> uuid({
582+
String? errorText,
583+
}) =>
584+
(valueCandidate) =>
585+
true == valueCandidate?.isNotEmpty && !isUUID(valueCandidate!)
586+
? errorText ?? FormBuilderLocalizations.current.uuidErrorText
587+
: null;
588+
589+
/// [FormFieldValidator] that requires the field's value to be valid json.
590+
static FormFieldValidator<String> json({
591+
String? errorText,
592+
}) =>
593+
(valueCandidate) =>
594+
true == valueCandidate?.isNotEmpty && !isJSON(valueCandidate!)
595+
? errorText ?? FormBuilderLocalizations.current.jsonErrorText
596+
: null;
597+
598+
/// [FormFieldValidator] that requires the field's value to be a valid latitude.
599+
static FormFieldValidator<String> latitude({
600+
String? errorText,
601+
}) =>
602+
(valueCandidate) =>
603+
true == valueCandidate?.isNotEmpty && !isLatitude(valueCandidate!)
604+
? errorText ?? FormBuilderLocalizations.current.latitudeErrorText
605+
: null;
606+
607+
/// [FormFieldValidator] that requires the field's value to be a valid longitude.
608+
static FormFieldValidator<String> longitude({
609+
String? errorText,
610+
}) =>
611+
(valueCandidate) =>
612+
true == valueCandidate?.isNotEmpty && !isLongitude(valueCandidate!)
613+
? errorText ?? FormBuilderLocalizations.current.longitudeErrorText
614+
: null;
615+
616+
/// [FormFieldValidator] that requires the field's value to be a valid base64 string.
617+
static FormFieldValidator<String> base64({
618+
String? errorText,
619+
}) =>
620+
(valueCandidate) =>
621+
true == valueCandidate?.isNotEmpty && !isBase64(valueCandidate!)
622+
? errorText ?? FormBuilderLocalizations.current.base64ErrorText
623+
: null;
624+
625+
/// [FormFieldValidator] that requires the field's value to be a valid file or folder path.
626+
static FormFieldValidator<String> path({
627+
String? errorText,
628+
}) =>
629+
(valueCandidate) =>
630+
true == valueCandidate?.isNotEmpty && !isFilePath(valueCandidate!)
631+
? errorText ?? FormBuilderLocalizations.current.pathErrorText
632+
: null;
633+
634+
/// [FormFieldValidator] that requires the field's value to be an odd number.
635+
static FormFieldValidator<String> oddNumber({
636+
String? errorText,
637+
}) =>
638+
(valueCandidate) =>
639+
true == valueCandidate?.isNotEmpty && !isOddNumber(valueCandidate!)
640+
? errorText ?? FormBuilderLocalizations.current.oddNumberErrorText
641+
: null;
642+
643+
/// [FormFieldValidator] that requires the field's value to be an even number.
644+
static FormFieldValidator<String> evenNumber({
645+
String? errorText,
646+
}) =>
647+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
648+
!isEvenNumber(valueCandidate!)
649+
? errorText ?? FormBuilderLocalizations.current.evenNumberErrorText
650+
: null;
651+
652+
/// [FormFieldValidator] that requires the field's value to be a valid port number.
653+
static FormFieldValidator<String> portNumber({
654+
int min = 0,
655+
int max = 65535,
656+
String? errorText,
657+
}) =>
658+
(valueCandidate) {
659+
if (true == valueCandidate?.isNotEmpty) {
660+
int? port = int.tryParse(valueCandidate!);
661+
if (port == null || port < min || port > max) {
662+
return errorText ??
663+
FormBuilderLocalizations.current.portNumberErrorText(min, max);
664+
}
665+
} else {
666+
return errorText ??
667+
FormBuilderLocalizations.current.portNumberErrorText(min, max);
668+
}
669+
return null;
670+
};
671+
672+
/// [FormFieldValidator] that requires the field's value to be a valid macAddress.
673+
static FormFieldValidator<String> macAddress({
674+
String? errorText,
675+
}) =>
676+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
677+
!isMACAddress(valueCandidate!)
678+
? errorText ?? FormBuilderLocalizations.current.macAddressErrorText
679+
: null;
680+
681+
/// [FormFieldValidator] that requires the field's value to starts with a specific value.
682+
/// * [prefix] is the value that the field's value should start with.
683+
static FormFieldValidator<String> startsWith({
684+
required String prefix,
685+
String? errorText,
686+
}) =>
687+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
688+
!valueCandidate!.startsWith(prefix)
689+
? errorText ??
690+
FormBuilderLocalizations.current.startsWithErrorText(prefix)
691+
: null;
692+
693+
/// [FormFieldValidator] that requires the field's value to ends with a specific value.
694+
/// * [suffix] is the value that the field's value should end with.
695+
static FormFieldValidator<String> endsWith({
696+
required String suffix,
697+
String? errorText,
698+
}) =>
699+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
700+
!valueCandidate!.endsWith(suffix)
701+
? errorText ??
702+
FormBuilderLocalizations.current.endsWithErrorText(suffix)
703+
: null;
704+
705+
/// [FormFieldValidator] that requires the field's value to contains a specific value.
706+
/// * [substring] is the value that the field's value should contain.
707+
/// * [caseSensitive] is a `bool` that sets if the search is case sensitive. By default `true`
708+
static FormFieldValidator<String> contains({
709+
required String substring,
710+
String? errorText,
711+
}) =>
712+
(valueCandidate) => true == valueCandidate?.isNotEmpty &&
713+
!valueCandidate!.contains(substring)
714+
? errorText ??
715+
FormBuilderLocalizations.current.containsErrorText(substring)
716+
: null;
717+
718+
/// [FormFieldValidator] that requires the field's value to be between two numbers.
719+
/// * [min] is the minimum value that the field's value should be greater than or equal to.
720+
/// * [max] is the maximum value that the field's value should be less than or equal to.
721+
static FormFieldValidator<num> between({
722+
required num min,
723+
required num max,
724+
String? errorText,
725+
}) =>
726+
(valueCandidate) =>
727+
valueCandidate == null || valueCandidate < min || valueCandidate > max
728+
? errorText ??
729+
FormBuilderLocalizations.current.betweenErrorText(min, max)
730+
: null;
731+
732+
/// [FormFieldValidator] that requires the field's value to in a list of values.
733+
/// * [values] is the list of values that the field's value should be in.
734+
static FormFieldValidator<T> inList<T>(
735+
List<T> values, {
736+
String? errorText,
737+
}) =>
738+
(valueCandidate) => !values.contains(valueCandidate)
739+
? errorText ?? FormBuilderLocalizations.current.valueInListErrorText
740+
: null;
570741
}

0 commit comments

Comments
 (0)