Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: decouple purity, validity, and submission status #48

Merged

Conversation

felangel
Copy link
Contributor

@felangel felangel commented Jan 17, 2022

Description

This pull request attempts to decouple purity from validity (closes #45) as well as to decouple submission status from validity/purity (closes #46).

Changes

  1. FormzStatus renamed to FormzSubmissionStatus:
/// Enum representing the submission status of a form.
enum FormzSubmissionStatus {
  /// The form is in the process of being submitted.
  inProgress,

  /// The form has been submitted successfully.
  success,

  /// The form submission failed.
  failure,

  /// The form submission has been canceled.
  canceled
}
  1. FormzInput class no longer exposes a status (FormzInputStatus). Instead there are isValid and isNotValid getters:
class NameInput extends FormzInput<String, NameInputError> {
  const NameInput.pure() : super.pure('');
  const NameInput.dirty({String value = ''}) : super.dirty(value);

  @override
  NameInputError? validator(String value) {
    return value.isNotEmpty == true ? null : NameInputError.empty;
  }
}

void main() {
  const name = NameInput.pure();
  print(name.isValid); // false
  print(name.isNotValid); // true

  const joe = NameInput.dirty(value: 'joe');
  print(joe.isValid); // true
  print(joe.isNotValid); // false
}
  1. FormzInput has a displayError getter which returns an error to display if the input is not valid and has been modified by the user (closes Determining when to show meaningful errors #44)
void main() {
  const name = NameInput.pure();
  print(name.displayError); // null

  const invalid = NameInput.dirty(value: '');
  print(name.displayError); // NameInputError.empty
}
  1. Renamed pure to isPure for consistency

  2. Use very_good_analysis

@felangel felangel added refactor A code change that neither fixes a bug nor add a feature breaking change labels Jan 17, 2022
@felangel felangel self-assigned this Jan 17, 2022
@codecov-commenter
Copy link

codecov-commenter commented Jan 17, 2022

Codecov Report

Merging #48 (b3fb409) into main (26a22df) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff            @@
##              main       #48   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           31        27    -4     
=========================================
- Hits            31        27    -4     
Impacted Files Coverage Δ
lib/formz.dart 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 26a22df...b3fb409. Read the comment docs.

Copy link
Contributor

@jorgecoca jorgecoca left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the simplification! 💯

lib/formz.dart Show resolved Hide resolved
Copy link

@definitelyokay definitelyokay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank goodness ❤️

@felangel felangel merged commit bf4d6fc into main Jan 18, 2022
@felangel felangel deleted the refactor/decouple-purity-validity-and-submission-status branch January 18, 2022 16:33
@MJ12358
Copy link

MJ12358 commented Mar 17, 2022

Love the changes! Thanks for this!
Is there any chance on making this a release and publishing to pub.dev?

@felangel
Copy link
Contributor Author

Love the changes! Thanks for this!
Is there any chance on making this a release and publishing to pub.dev?

Thanks! Yup will try to get a release out shortly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
refactor A code change that neither fixes a bug nor add a feature
Projects
None yet
5 participants