Error prone nested MultiValidator implementation
nipunasudha opened this issue · comments
ParagonTheDev commented
I hunted for 2 hours to understand why my field error messages are going nuts. And this is why.
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;
}
}