validateAndSave method

void validateAndSave({
  1. required Future<SaveResult> saveWork()?,
  2. void onFailure(
    1. SaveResult result
    )?,
  3. void onSuccess(
    1. SaveResult result
    )?,
})

Implementation

void validateAndSave({
  required Future<SaveResult> Function()? saveWork,
  void Function(SaveResult result)? onFailure,
  void Function(SaveResult result)? onSuccess}) async
{
  fieldErrors.clear();

  bool success = validate();
  if (!success) {
    return;
  }

  if (saveWork == null) {
    return;
  }

  SaveResult result = await saveWork();

  if (result.status == SaveStatus.validationFailed) {
    if (result.hasFieldErrors) {
      fieldErrors.addAll(result.fieldErrors!);
      validate();
    }
  }

  Function(SaveResult)? fn = result.success ? onSuccess : onFailure;
  if (fn != null) {
    fn(result);
  }
}