joanpablo / reactive_forms

This is a model-driven approach to handling form inputs and validations, heavily inspired in Angular's Reactive Forms
MIT License
471 stars 90 forks source link

_runAsyncValidators() should run even if the ControlStatus is invalid. #433

Closed BenjiFarquhar closed 10 months ago

BenjiFarquhar commented 11 months ago

It is flawed logic to not run _asyncValidators if the control is valid. If the control only has one validator and it is async, and the async validator has run and put the control in an invalid state, then changing the control value won't re-run the async validator and it will forever remain invalid.

  void updateValueAndValidity({
    bool updateParent = true,
    bool emitEvent = true,
  }) {
    _setInitialStatus();
    _updateValue();
    if (enabled) {
      _cancelExistingSubscription();
      _errors = _runValidators();
      _status = _calculateStatus();
      if (_status == ControlStatus.valid || _status == ControlStatus.pending) {
        _runAsyncValidators();
      }
    }

    if (emitEvent) {
      _valueChanges.add(value);
      _statusChanges.add(_status);
    }

    _updateAncestors(updateParent);
  }