Milad-Akarie / form_field_validator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error prone nested MultiValidator implementation

nipunasudha opened this issue · comments

I hunted for 2 hours to understand why my field error messages are going nuts. And this is why.

Screenshot 2021-08-22 at 12 03 21

I use nested MultiValidator instances and they are malfunctioning due to this static field. Please fix this. I can't do it without changing several base classes because you have made the errorText field final!

Here's an improved version of TypedMultiValidator I use, which also has type support.

import 'package:form_field_validator/form_field_validator.dart';

class TypedMultiValidator<T> extends FieldValidator<T> {
  final List<FieldValidator<T>> validators;

  // error test is no longer static
  String _errorText = '';

  // the empty string passed to super is not used
  TypedMultiValidator(this.validators) : super('');

  @override
  bool isValid(value) {
    for (FieldValidator<T> validator in validators) {
      // ignores null validators
      if (validator == null) continue;
      // uses the called output instead of using raw error message
      final validationResult = validator.call(value);
      if (validationResult != null) {
        _errorText = validationResult;
        return false;
      }
    }
    return true;
  }

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