valido method

String? valido(
  1. String? valor, {
  2. dynamic clearNoNumber = false,
  3. dynamic seNulo,
})

Implementation

String? valido(String? valor, {clearNoNumber = false, seNulo }) {
  bool isNotNull = (valor != null);

  if (clearNoNumber) {
    valor = valor!.replaceAll(RegExp(r'[^0-9]'), '');
  }

  if(!_lista.values.contains(Validar.OBRIGATORIO_SE_NULO)){
    if (this._equals != null) {
      if (!isNotNull || valor != this._equals) {
        _erros.add(this._equalsMsg);
      }
    }
  }

  if (this._minVal != null) {
    try {
      if (!isNotNull || int.parse(valor!) < this._minVal!) {
        _erros.add(this._minValMsg);
      }
    } catch (e) {
      _erros.add(this._minValMsg);
    }
  }

  if (this._maxVal != null) {
    try {
      if (!isNotNull || int.parse(valor!) > this._maxVal!) {
        _erros.add(this._maxValMsg);
      }
    } catch (e) {
      _erros.add(this._maxValMsg);
    }
  }

  if (this._minLength != null) {
    if (!isNotNull || valor!.length < this._minLength!) {
      _erros.add(this._minLengthMsg);
    }
  }

  if (this._maxLength != null) {
    if (!isNotNull || valor!.length > this._maxLength!) {
      _erros.add(this._maxLengthMsg);
    }
  }

  if (this._isBool != null) {
    if (!isNotNull || !this._isBool!) {
      _erros.add(this._isBoolMsg);
    }
  }


  _lista.forEach((validar, msg) {
    switch (validar) {

      case Validar.OBRIGATORIO:
        if (!isNotNull || valor!.trim().isEmpty) {
          _erros.add(msg);
        }
        break;

      case Validar.OBRIGATORIO_SE_NULO :
        if (seNulo==null) {
          if (!isNotNull || valor!.trim().isEmpty) {
            _erros.add(msg);
          }
        }

        break;
      case Validar.CPF:
        if (!CPF.isValid(valor)) {
          _erros.add(msg);
        }

        break;

      case Validar.CNPJ:
        if (!CNPJ.isValid(valor)) {
          _erros.add(msg);
        }
        break;

      case Validar.EMAIL:
        if (!EmailValidator.validate(valor)) {
          _erros.add(msg);
        }
        break;
      default:
    }
  });

  if (_erros.length > 0) {
    return _erros.toString();
  }

  return null;
}