validate method

Future<FormeFieldValidateSnapshot<T?>> validate({
  1. bool quietly = false,
})
inherited

this method is used to manually validate

Implementation

Future<FormeFieldValidateSnapshot<T>> validate({bool quietly = false}) async {
  final T value = this.value;
  if (!_hasAnyValidator || !enabled) {
    return FormeFieldValidateSnapshot(
        value, FormeFieldValidation.unnecessary, name, this);
  }
  final int gen = quietly ? _validateGen : ++_validateGen;

  bool isValid() {
    return mounted && gen == _validateGen;
  }

  bool needNotify() {
    return !quietly && isValid();
  }

  void notify(FormeFieldValidation validation) {
    if (needNotify() && _status.validation != validation) {
      setState(() {
        _status = _status._copyWith(validation: FormeOptional(validation));
      });
    }
  }

  if (_hasValidator) {
    final String? errorText = widget.validator!(this, value);
    if (errorText != null || !_hasAsyncValidator) {
      final FormeFieldValidation validation =
          _createFormeFieldValidation(errorText);
      notify(validation);
      return FormeFieldValidateSnapshot(value, validation, name, this);
    }
  }

  notify(FormeFieldValidation.validating);

  FormeFieldValidation validation;
  try {
    final String? errorText =
        await widget.asyncValidator!(this, value, isValid);
    validation = _createFormeFieldValidation(errorText);
  } catch (e, stackTrace) {
    validation = FormeFieldValidation.fail(e, stackTrace);
  }

  notify(validation);
  return FormeFieldValidateSnapshot(value, validation, name, this);
}