搜索插件、代码仓与 APP 分类

插件详情v1.1.1

formdator

一组表单字段验证器,可通过组合选择并组合成各种组合——装饰器模式。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
117
喜欢数
28
Pub 分数
150

安装插件

flutter pub add formdator

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

Formdator 是一个 Dart 包,提供了一组灵活的表单字段验证器,可使用装饰器模式组合在一起。它使开发者能够无缝地组合多个验证规则,从而在表单中实现可重用且模块化的验证逻辑。适用于需要动态或复杂输入验证的应用程序,支持移动、Web 和桌面等多种平台。该包强调代码简洁、可组合性,并能轻松集成到 Flutter 的表单生态系统中。

适合场景

  • 动态表单验证
  • 复杂输入规则
  • 可重用的验证组件
  • 跨平台表单
  • 自定义验证链
  • 实时反馈

主要特点

  • 可组合的验证器
  • 装饰器模式实现
  • Platform-agnostic
  • 与 Flutter 表单轻松集成
  • 模块化设计
  • 可扩展的验证逻辑

适合对象

  • 开发复杂表单的开发者
  • 需要可维护验证代码的团队
  • 需要多规则验证的应用
  • 具有跨平台需求的 Flutter 项目
  • 注重代码复用的项目

使用建议

  • 需要理解装饰器模式
  • 可能增加简单表单的复杂性
  • 除了基本示例外,文档有限
  • 依赖于表单控件中的正确设置

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

formdator

EO-Color logo

EO principles respected here DevOps By Rultor.com

pub license PDD status

build codecov CodeFactor Grade style: lint Hits-of-Code

Contents

Overview

Formdator — Formidable Validator.

Formdator is a fully object-oriented package for validating Flutter form fields. Its main benefits, compared to all other similar packages, include:

  • Dependency-free: there is only pure Dart code.
  • Object-oriented mindset: the elements for validation are immutable objects that can be combined in various configurations.
  • Classes with short — yet meaningful — names like Req for required fields; ReqEmail for non-empty, well-formed emails; Len for length constraints; Int for integer-only values; and so on.
  • Easy-to-compose validators: e.g., the command Trim(Email()) produces a validator that trims an email value before validating it.
  • Multiple validation at once: you can apply multiple validation rules at once by using the Pair or Rules classes.
  • Built-in set of compound validators: e.g., to validate an email and limit its length to a maximum of 50 characters, simply use an instance of Email.len(50) — write less; do more!

For easier integration with Flutter form fields, every validator implements the call() method. As a result, any validator object can be called as a function — Callable Classes.

Getting Started

The following code snippet demonstrates how you can easily group the Rules, Req, Len and Email classes together to create a kind of 'email-max-50-characters' constraint.

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      validator: Rules<String>([ // The "Rules" class performs multiple validations at once.
        Req(blank: 'Please enter the email'), // "blank" is the error message in case of field left blank.
        Len.max(50, long: 'Email length cannot exceed 50 characters'), // "long" is the error message if an input value is too long.
        Email(mal: 'Malformed email'), // "mal" is the error message in case of malformed email.
      ]),
    );
  }

Or — even better — use the compound validator ReqEmail to perform the same task.

  @override
  Widget build(BuildContext context) {
    return TextFormField(
      validator: ReqEmail.len(
        50,
        blank: 'Please enter the email',
        mal: 'Malformed email',
        long: 'Email length cannot exceed 50 characters',
      ),
    );
  }

The shorter command ReqEmail.len(50) is equivalent to the much longer command Rules<String>([Req(), Len.max(50), Email()]) — write less; do more!

List of Validators

For a complete list of validators with detailed information about each one (constructors, parameters, etc.):

Grouped by Category
  • brazil — validators related to Brazil (BrMobile, BrPhone, Cep, Cnpj, Cpf, etc.).
  • core — core validators (Len, Match [text pattern], Pair, Req, Rules, Trim, etc.).
  • logic — validation logic and unit testing (Equal, Ok, Nok, ValueBack).
  • net — internet (Email, Ipv4, Ipv6, MacAddr, Url, etc.).
  • numeric — validators related to numbers or digits (Digit, Hex, Int, Num, etc.).

Demo application

The demo application provides a fully working example, focused on demonstrating exactly four validators in action — Pair, ReqLen, ReqEmail, and Equal. You can take the code in this demo and experiment with it.

To run the demo application:

  git clone https://github.com/dartoos-dev/formdator.git
  cd formdator/example/
  flutter run -d chrome

This should launch the demo application on Chrome in debug mode.

formdator-demo-app

Contribute

Contributors are welcome!

  1. Open an issue regarding an improvement, a bug you noticed, or ask to be assigned to an existing one.
  2. If the issue is confirmed, fork the repository, do the changes on a separate branch and make a Pull Request.
  3. After review and acceptance, the Pull Request is merged and closed.

Make sure the command below passes before making a Pull Request.

  dart analyze && sudo dart test

References

所属分类

源码仓库

使用此插件的项目