handle method

  1. @override
bool handle(
  1. Map<String, dynamic> info
)
override

Handle the validation, the info variable will contain the following: info'rule' = Validation rule i.e "min". info'data' = Data the user has passed into the validation. info'message' = Overriding message to be displayed for validation (optional).

Implementation

@override
bool handle(Map<String, dynamic> info) {
  RegExp regExp = RegExp(this.signature + r':([0-9]+)');
  String match = regExp.firstMatch(info['rule'])?.group(1) ?? "";
  int intMatch = int.parse(match);

  dynamic data = info['data'];
  if (data is String) {
    // Check if the string is a number
    if (double.tryParse(data) != null) {
      this.description = "$attribute must be a maximum of $intMatch.";
      this.textFieldMessage = "Must be a maximum of $intMatch.";
      super.handle(info);
      return (double.tryParse(data)! < intMatch);
    } else {
      this.description =
          "$attribute must be a maximum length of $intMatch characters.";
      this.textFieldMessage = "Must be a maximum of $intMatch characters.";
      super.handle(info);
      return (data.length < intMatch);
    }
  }

  if (data is int) {
    this.description = "$attribute must be a maximum of $intMatch.";
    this.textFieldMessage = "Must be a maximum of $intMatch.";
    super.handle(info);
    return (data < intMatch);
  }

  if (data is List) {
    this.description = "$attribute must be a maximum of $intMatch.";
    this.textFieldMessage = "Must be a maximum of $intMatch.";
    super.handle(info);
    return (data.length < intMatch);
  }

  if (data is Map) {
    this.description = "$attribute must be a maximum of $intMatch.";
    this.textFieldMessage = "Must be a maximum of $intMatch.";
    super.handle(info);
    return (data.length < intMatch);
  }

  if (data is double) {
    this.description = "$attribute must be a maximum of $intMatch.";
    this.textFieldMessage = "Must be a maximum of $intMatch.";
    super.handle(info);
    return (data < intMatch);
  }

  return false;
}