Milad-Akarie / form_field_validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Is there a way to use MultiValidator with other data types?

yasht01 opened this issue · comments

MultiValidator accepts only String? values

image

Workaround solution for now:

class CustomMultiValidator<T> extends FieldValidator<T?> {
  final List<FieldValidator> validators;
  static String _errorText = '';

  CustomMultiValidator(this.validators) : super(_errorText);

  @override
  bool isValid(value) {
    for (FieldValidator validator in validators) {
      if (validator.call(value) != null) {
        _errorText = validator.errorText;
        return false;
      }
    }
    return true;
  }

  @override
  String? call(T? value) {
    return isValid(value) ? null : _errorText;
  }
}