validator method

StreamSubscription<DOMEvent> validator(
  1. Function validator, {
  2. String? errorClass,
  3. String? validClass,
})

Sets the validator of this DOMElement.

Implementation

StreamSubscription<DOMEvent> validator(Function validator,
    {String? errorClass, String? validClass}) {
  return onChange.listen((_) {
    Object? result;

    var rt = runtime;

    if (validator is Function(DOMElement)) {
      result = validator(this);
    } else if (validator is Function(String?)) {
      var value = rt.value;
      result = validator(value);
    } else if (validator is Function(String)) {
      var value = rt.value ?? '';
      result = validator(value);
    } else if (validator is Function(Object)) {
      var node = rt.node;
      result = validator(node);
    }

    var valid = parseBool(result, false)!;

    if (valid) {
      if (errorClass != null) rt.removeClass(errorClass);
      if (validClass != null) rt.addClass(validClass);
    } else {
      if (validClass != null) rt.removeClass(validClass);
      if (errorClass != null) rt.addClass(errorClass);
    }
  });
}