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

minLength validation only when not empty #259

Closed
awhitford opened this issue Apr 20, 2020 · 8 comments
Closed

minLength validation only when not empty #259

awhitford opened this issue Apr 20, 2020 · 8 comments
Labels
enhancement New feature or request

Comments

@awhitford
Copy link
Collaborator

I only want the minLength validation when a value is specified; an empty field is fine.

It seems that this is arguably redundant:

            validators: [
              FormBuilderValidators.required(),
              FormBuilderValidators.minLength(4),
            ],

I thought I could simply remove the required() validator, but minLength(4) still complains that the empty field is not at least 4 characters.

Maybe the minLength validator could have an optional flag to say whether it should apply to empty fields? (That way, backwards compatibility can be maintained.)

@awhitford
Copy link
Collaborator Author

My workaround is to do this:

              validators: [
                (val) => val.isNotEmpty
                    ? FormBuilderValidators.minLength(4).call(val)
                    : null,
              ],

This is not awful, but I'm open to a more elegant solution.

@danvick
Copy link
Collaborator

danvick commented Apr 21, 2020

I only want the minLength validation when a value is specified; an empty field is fine.

It seems that this is arguably redundant:

            validators: [
              FormBuilderValidators.required(),
              FormBuilderValidators.minLength(4),
            ],

I thought I could simply remove the required() validator, but minLength(4) still complains that the empty field is not at least 4 characters.

Maybe the minLength validator could have an optional flag to say whether it should apply to empty fields? (That way, backwards compatibility can be maintained.)

This actually sounds like an elegant suggestion, I'll consider having it added.

@awhitford
Copy link
Collaborator Author

When the field is truly required, adding FormBuilderValidators.required(), provides a better user interface experience. It is better that it says, "field must not be empty", rather than just a "field must be at least 4 characters."

However, when the field is not required, adding the flexibility to FormBuilderValidators.minLength would be nice. Maybe something like:

validators: [
              FormBuilderValidators.minLength(4, allowEmpty: true),
            ],

@danvick danvick added the enhancement New feature or request label May 1, 2020
@danvick danvick closed this as completed in 8602975 May 3, 2020
@awhitford
Copy link
Collaborator Author

I just tried using this and it did not work:

    FormBuilderValidators.minLength(4, allowEmpty: true),

Alas, I get a "Value must have a length greater than or equal to 4." message when the field is blank.

The implementation is presently:

static FormFieldValidator minLength(
  num minLength, {
  bool allowEmpty = false,
  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) {
      return errorText ??
          "Value must have a length greater than or equal to $minLength";
    }
    return null;
  };
}

May I recommend this alternative:

static FormFieldValidator minLength(
  num minLength, {
  bool allowEmpty = false,
  String errorText,
}) {
  return (valueCandidate) {
    final valueLength = valueCandidate?.length ?? 0;
    if (valueLength < minLength && (!allowEmpty || valueLength > 0)) {
      return errorText ??
          "Value must have a length greater than or equal to $minLength";
    }
    return null;
  };
}

@danvick danvick reopened this May 6, 2020
@danvick
Copy link
Collaborator

danvick commented May 6, 2020

HI @awhitford,
I now see the mistake I made.

Could you be kind enough to submit a PR for this?

@awhitford
Copy link
Collaborator Author

Note that I see that you added allowEmpty to the maxLength validator too, but that is not necessary because an empty value will not trigger a max length validation.

@awhitford
Copy link
Collaborator Author

PR has been submitted. However, please note that I was unable to completely validate it because the build is broken:

../lib/src/fields/form_builder_phone_field.dart:181:11: Error: No named parameter with the name 'visualDensity'.

I assume that this is work in progress.

@awhitford
Copy link
Collaborator Author

Confirmed that this now works great with version 3.10.0. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants