formz 0.2.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 92

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

0.2.0 #

  • Remove redundant extensions on FormzInputStatus
  • Add isValidated extension on FormzStatus
  • Add invalid getter to FormzInput

0.1.0 #

  • Add extensions on FormzStatus
  • Add extensions on FormzInputStatus

0.0.1 #

Initial Release of the formz.

example/main.dart

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;
  }
}

void main() {
  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);

  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
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  formz: ^0.2.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:formz/formz.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
84
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
92
Learn more about scoring.

We analyzed this package on Jul 7, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.13

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.7.0 <3.0.0
Dev dependencies
effective_dart ^1.2.0
test ^1.14.2
test_coverage ^0.4.1