validate method

dynamic validate({
  1. required Map<String, dynamic> rules,
  2. Map<String, dynamic>? data,
  3. Map<String, dynamic>? messages,
  4. bool showAlert = true,
  5. Duration? alertDuration,
  6. ToastNotificationStyleType alertStyle = ToastNotificationStyleType.warning,
  7. required dynamic onSuccess()?,
  8. dynamic onFailure(
    1. Exception exception
    )?,
  9. String? lockRelease,
})

Validate data from your widget.

Implementation

validate(
    {required Map<String, dynamic> rules,
    Map<String, dynamic>? data,
    Map<String, dynamic>? messages,
    bool showAlert = true,
    Duration? alertDuration,
    ToastNotificationStyleType alertStyle =
        ToastNotificationStyleType.warning,
    required Function()? onSuccess,
    Function(Exception exception)? onFailure,
    String? lockRelease}) {
  if (!mounted) return;

  Map<String, String> finalRules = {};
  Map<String, dynamic> finalData = {};
  Map<String, dynamic> finalMessages = messages ?? {};

  bool completed = false;
  for (MapEntry rule in rules.entries) {
    String key = rule.key;
    dynamic value = rule.value;

    if (value is FormValidator) {
      FormValidator formValidator = value;

      if (formValidator.customRule != null) {
        bool passed = formValidator.customRule!(formValidator.data);
        if (!passed) {
          ValidationRule customRule = ValidationRule(
            signature: "FormValidator.custom",
            textFieldMessage: formValidator.message,
            title: formValidator.message ?? "Invalid data",
          );
          ValidationException exception =
              ValidationException(key, customRule);
          NyLogger.error(exception.toString());
          if (onFailure == null) return;
          onFailure(exception);
          completed = true;
          break;
        }
      }

      if (formValidator.rules == null) continue;
      finalRules[key] = formValidator.rules;
      finalData[key] = formValidator.data;
      if (formValidator.message != null) {
        finalMessages[key] = formValidator.message;
      }
      continue;
    }

    if (value is List) {
      assert(value.length < 4,
          'Validation rules can contain a maximum of 3 items. E.g. "email": [emailData, "add|validation|rules", "my message"]');
      finalRules[key] = value[1];
      finalData[key] = value[0];
      if (value.length == 3) {
        finalMessages[key] = value[2];
      }
    } else {
      if (value != null) {
        finalRules[key] = value;
      }
    }
  }

  if (completed) return;

  if (data != null) {
    data.forEach((key, value) {
      finalData.addAll({key: value});
    });
  }

  if (lockRelease != null) {
    this.lockRelease(lockRelease, perform: () async {
      try {
        NyValidator.check(
          rules: finalRules,
          data: finalData,
          messages: finalMessages,
          context: context,
          showAlert: showAlert,
          alertDuration: alertDuration,
          alertStyle: alertStyle,
        );

        if (onSuccess == null) return;
        await onSuccess();
      } on Exception catch (exception) {
        if (onFailure == null) return;
        onFailure(exception);
      }
    });
    return;
  }
  try {
    NyValidator.check(
      rules: finalRules,
      data: finalData,
      messages: finalMessages,
      context: context,
      showAlert: showAlert,
      alertDuration: alertDuration,
      alertStyle: alertStyle,
    );

    if (onSuccess == null) return;
    onSuccess();
  } on Exception catch (exception) {
    NyLogger.error(exception.toString());
    if (onFailure == null) return;
    onFailure(exception);
  }
}