Skip to content

A unified form representation in Dart. Aims to simplify form representation and validation in a generic way.

License

Notifications You must be signed in to change notification settings

jezsung/formz

This branch is 53 commits behind VeryGoodOpenSource/formz:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

51da5ba ยท Apr 26, 2021

History

15 Commits
Jan 10, 2021
Apr 26, 2021
Apr 26, 2021
Apr 26, 2021
May 8, 2020
Apr 26, 2021
May 8, 2020
May 8, 2020
Jan 10, 2021
Jul 30, 2020
Apr 26, 2021

Repository files navigation

Formz ๐Ÿ“

Build codecov Pub style: effective dart License: MIT


A unified form representation in Dart. Formz aims to simplify form representation and validation in a generic way.

Create a FormzInput

import 'package:formz/formz.dart';

// Define input validation errors
enum NameInputError { empty }

// Extend FormzInput and provide the input type and error type.
class NameInput extends FormzInput<String, NameInputError> {
  // Call super.pure to represent an unmodified form input.
  const NameInput.pure() : super.pure('');

  // Call super.dirty to represent a modified form input.
  const NameInput.dirty({String value = ''}) : super.dirty(value);

  // Override validator to handle validating a given input value.
  @override
  NameInputError validator(String value) {
    return value?.isNotEmpty == true ? null : NameInputError.empty;
  }
}

Interact with a FormzInput

final name = NameInput.pure();
print(name.value); // ''
print(name.valid); // false
print(name.status); // FormzInputStatus.pure
print(name.error); // NameInputError.empty

final joe = NameInput.dirty(value: 'joe');
print(joe.value); // 'joe'
print(joe.valid); // true
print(joe.status); // FormzInputStatus.valid
print(joe.error); // null
print(joe.toString()); // NameInput('joe', true);

Validate Multiple FormzInput Items

final validInputs = <FormzInput>[
  NameInput.dirty(value: 'jan'),
  NameInput.dirty(value: 'jen'),
  NameInput.dirty(value: 'joe'),
];

print(Formz.validate(validInputs)); // FormzStatus.valid

final invalidInputs = <FormzInput>[
  NameInput.dirty(value: ''),
  NameInput.dirty(value: ''),
  NameInput.dirty(value: ''),
];

print(Formz.validate(invalidInputs)); // FormzStatus.invalid

Automatic FormzStatus Computation

class LoginForm with FormzMixin {
  LoginForm({
    this.username = const Username.pure(),
    this.password = const Password.pure(),
  });

  final Username username;
  final Password password;

  @override
  List<FormzInput> get inputs => [username, password];
}

final form = LoginForm();
print(form.status); // FormzStatus.pure

About

A unified form representation in Dart. Aims to simplify form representation and validation in a generic way.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Dart 100.0%