Coder Social home page Coder Social logo

mamuy-gu / formz Goto Github PK

View Code? Open in Web Editor NEW

This project forked from verygoodopensource/formz

0.0 1.0 0.0 25 KB

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

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

License: MIT License

Dart 100.00%

formz's Introduction

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

formz's People

Contributors

felangel avatar jorgecoca avatar enyo avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.