Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Corrected minLength and maxLength validators. #273

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions lib/src/form_builder_validators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,8 @@ class FormBuilderValidators {
String errorText,
}) {
return (valueCandidate) {
if (allowEmpty && (valueCandidate == null || valueCandidate?.length == 0))
return errorText ??
"Value must have a length greater than or equal to $minLength";
if (valueCandidate != null && valueCandidate.length < minLength) {
final valueLength = valueCandidate?.length ?? 0;
if (valueLength < minLength && (!allowEmpty || valueLength > 0)) {
return errorText ??
"Value must have a length greater than or equal to $minLength";
}
Expand All @@ -92,13 +90,8 @@ class FormBuilderValidators {
static FormFieldValidator maxLength(
num maxLength, {
String errorText,
bool allowEmpty = false,
}) {
return (valueCandidate) {
if (allowEmpty && (valueCandidate == null || valueCandidate?.length == 0))
return errorText ??
"Value must have a length greater than or equal to $minLength";

if (valueCandidate != null && valueCandidate.length > maxLength) {
return errorText ??
"Value must have a length less than or equal to $maxLength";
Expand Down