equalLength<T> static method

FormFieldValidator<T> equalLength<T>(
  1. int length, {
  2. bool allowEmpty = false,
  3. String? errorText,
})

Implementation

static FormFieldValidator<T> equalLength<T>(int length, {
  bool allowEmpty = false,
  String? errorText,
}) {
  assert(length > 0);
  return (T? valueCandidate) {
    assert(valueCandidate is String ||
        valueCandidate is Iterable ||
        valueCandidate is int ||
        valueCandidate == null);
    int valueLength = 0;

    if (valueCandidate is int) valueLength = valueCandidate.toString().length;
    if (valueCandidate is String) valueLength = valueCandidate.length;
    if (valueCandidate is Iterable) valueLength = valueCandidate.length;

    return valueLength != length && (!allowEmpty || valueLength > 0)
        ? errorText ?? BasicFormValidatorMessageError().equalLength!(length)
        : null;
  };
}