validateValue method

  1. @override
String? validateValue(
  1. String valueCandidate
)
override

Validates the value. Returns null if the value is valid, otherwise an error message. Call validate() instead of this method when using the validator.

Implementation

@override
String? validateValue(String valueCandidate) {
  if (valueCandidate.length < minLength) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations.current.minLengthErrorText(minLength);
  }

  if (valueCandidate.length > maxLength) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations.current.maxLengthErrorText(maxLength);
  }

  if (!allowNumbers && RegExp('[0-9]').hasMatch(valueCandidate)) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations
              .current
              .usernameCannotContainNumbersErrorText;
  }

  if (!allowUnderscore && valueCandidate.contains('_')) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations
              .current
              .usernameCannotContainUnderscoreErrorText;
  }

  if (!allowDots && valueCandidate.contains('.')) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations.current.usernameCannotContainDotsErrorText;
  }

  if (!allowDash && valueCandidate.contains('-')) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations
              .current
              .usernameCannotContainDashesErrorText;
  }

  if (!allowSpace && valueCandidate.contains(' ')) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations
              .current
              .usernameCannotContainSpacesErrorText;
  }

  if (!allowSpecialChar &&
      RegExp(r'[!@#<>?":_`~;[\]\\|=+)(*&^%-]').hasMatch(valueCandidate)) {
    return errorText != FormBuilderLocalizations.current.usernameErrorText
        ? errorText
        : FormBuilderLocalizations
              .current
              .usernameCannotContainSpecialCharErrorText;
  }

  return null;
}