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

Separate purity from validity

definitelyokay opened this issue · comments

Formz currently (and incorrectly) conflates purity with validity.

Imagine a class which contains a username input field (which is un remarkable implementation of a FormzInput).

class RegistrationState {
  final UsernameInput username;
  bool get isBusy => ...
  bool get canSubmit =>
      Formz.validate([username]) == FormzStatus.valid && !isBusy;
}

Currently, the following test fails, despite the fact that unicorn is considered a valid string by the implementation of UsernameInput (not shown).

test('true when username is valid and not busy', () {
  const state = RegistrationState(
    username: UsernameInput.pure(value: 'unicorn'),
    isCheckingUsername: false,
    status: RegistrationStatus.editing,
  );
  expect(state.canSubmit, true);
});

The reason for the failure is because the value of Formz.validate([username]) is FormzStatus.pure, rather than FormzStatus.valid.

I believe FormzInput shoulds have a separate field which tracks the field's purity. Additionally, it may be suitable to have a displayError property which is null when the field is pure, as UI's commonly disregard errors on unmodified (but incorrect) fields.

abstract class FormzInput<T, E> {
  final E? get displayError => pure ? null : error;
  ...
}