VeryGoodOpenSource / formz

A unified form representation in Dart used at Very Good Ventures 🦄

Home Page:https://pub.dev/packages/formz

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to add submission related status to a FormMixin?

JoseGeorges8 opened this issue · comments

It seems like this could be a bug or is it intended? but I don't see how can you add submission related status to a class with the FormMixin

Same, if I'm using the FormMixin I can't set the status it only returns valid or invalid and the other statuses are unusable.. seems odd if intentional.

Hi @JoseGeorges8 👋
Thanks for opening an issue and yes this is currently a limitation of the FormMixin. Need to think some more about how to handle this use-case properly. If you have ideas/suggestions I'd love to hear them, thanks!

I was thinking on adding something like this to the mixin:

FormzStatus _status = FormzStatus.pure;

Future<bool> onSubmit();

Future<FormzStatus> get submit => _onUserSubmit(onSubmit());

 _onUserSubmit(Future<bool> onSubmit) async {
    assert(onSubmit != null);
    _status = FormzStatus.submissionInProgress;
    final success = await onSubmit;
    _status = success ? FormzStatus.submissionSuccess : FormzStatus.submissionFailure;
    return _status;
}

however as I was typing it I noticed how it won't be flexible. For example onSubmit logic will have to be written within the class which usually would be a state of a cubit, and it wouldn't make sense to have it in there...

I thought it still be good to bring this to the table in case it inspires anyone :)

Hey @felangel,
How about something like that?

enum FormzSubmissionStatus { pure, inProgress, finished }

mixin FormzMixin<E> {
  E? get submissionError;
  FormzSubmissionStatus get submissionStatus;

  FormzStatus get status {
    switch (submissionStatus) {
      case FormzSubmissionStatus.inProgress:
        return FormzStatus.submissionInProgress;
      case FormzSubmissionStatus.finished:
        return (null == submissionError)
            ? FormzStatus.submissionSuccess
            : FormzStatus.submissionFailure;
      case FormzSubmissionStatus.pure:
        return Formz.validate(inputs);
    }
  }

  List<FormzInput> get inputs;
}

As a bonus, it also solves #26.

I've refined it somewhat.

enum FormzSubmissionStatus { pure, inProgress, success, failure }

class FormzSubmission<E> {
  const FormzSubmission._(this.status, [this.error]);
  const FormzSubmission.pure() : this._(FormzSubmissionStatus.pure);
  const FormzSubmission.inProgress() : this._(FormzSubmissionStatus.inProgress);
  const FormzSubmission.success() : this._(FormzSubmissionStatus.success);
  const FormzSubmission.failure(E error)
      : this._(FormzSubmissionStatus.failure, error);

  final E? error;
  final FormzSubmissionStatus status;
}

mixin FormzMixin<E> {
  FormzStatus get status {
    switch (submission.status) {
      case FormzSubmissionStatus.inProgress:
        return FormzStatus.submissionInProgress;
      case FormzSubmissionStatus.success:
        return FormzStatus.submissionSuccess;
      case FormzSubmissionStatus.failure:
        return FormzStatus.submissionFailure;
      case FormzSubmissionStatus.pure:
        return Formz.validate(inputs);
    }
  }

  List<FormzInput> get inputs;
  FormzSubmission<E> get submission;
}

@felangel what do you think my suggestion above?
Should I create a PR?

Hi @NoamDev & @JoseGeorges8 👋 Thanks for being active on this ticket. Given that there has been no movement on this for over two years at this point I'm going to move this to closed. If you think this is still a relevant issue that needs to be solved please feel free to open a PR with the needed change so the team can help work with you through the submission and release process.

Closing as "won't fix"