Skip to content

Commit 929f63d

Browse files
authored
Corrected minLength and maxLength validators. (#273)
1 parent 58d7545 commit 929f63d

File tree

1 file changed

+2
-9
lines changed

1 file changed

+2
-9
lines changed

lib/src/form_builder_validators.dart

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,8 @@ class FormBuilderValidators {
7676
String errorText,
7777
}) {
7878
return (valueCandidate) {
79-
if (allowEmpty && (valueCandidate == null || valueCandidate?.length == 0))
80-
return errorText ??
81-
"Value must have a length greater than or equal to $minLength";
82-
if (valueCandidate != null && valueCandidate.length < minLength) {
79+
final valueLength = valueCandidate?.length ?? 0;
80+
if (valueLength < minLength && (!allowEmpty || valueLength > 0)) {
8381
return errorText ??
8482
"Value must have a length greater than or equal to $minLength";
8583
}
@@ -92,13 +90,8 @@ class FormBuilderValidators {
9290
static FormFieldValidator maxLength(
9391
num maxLength, {
9492
String errorText,
95-
bool allowEmpty = false,
9693
}) {
9794
return (valueCandidate) {
98-
if (allowEmpty && (valueCandidate == null || valueCandidate?.length == 0))
99-
return errorText ??
100-
"Value must have a length greater than or equal to $minLength";
101-
10295
if (valueCandidate != null && valueCandidate.length > maxLength) {
10396
return errorText ??
10497
"Value must have a length less than or equal to $maxLength";

0 commit comments

Comments
 (0)