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

插件详情v2.0.0

the_validator

一个简单易用的 Flutter 验证包,可用于验证表单字段、字符串和值

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
183
喜欢数
25
Pub 分数
130

安装插件

flutter pub add the_validator

插件介绍

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

验证器是一个轻量级的Flutter包,旨在实现简单高效的表单字段、字符串和值验证。它提供了一个简洁直观的API,用于验证电子邮件、密码和自定义条件等输入内容。非常适合希望快速集成而无需复杂设置的开发者,支持Android、iOS、Web、macOS、Windows和Linux等多个平台。该包通过在表单输入过程中提供实时反馈来提升用户体验。其极简的设计使其易于在任何需要基本或高级验证逻辑的Flutter项目中采用。

适合场景

  • 表单输入验证
  • 电子邮件和密码检查
  • 自定义验证规则
  • 实时用户反馈
  • 用户表单中的数据完整性
  • 跨平台验证

主要特点

  • 简单 API
  • 支持多种验证类型
  • 实时验证
  • 跨平台兼容性
  • 可扩展的验证规则
  • 轻量且快速

适合对象

  • 初学者 Flutter 开发者
  • 需要快速验证的项目
  • 移动和Web应用
  • 表单密集型应用
  • 具有严格数据录入要求的应用程序
  • 偏好最小依赖的开发者

使用建议

  • 仅限基本验证需求
  • 无内置国际化支持
  • 需要手动处理错误消息
  • 不太适合高度复杂的验证工作流
  • 依赖外部 GitHub 仓库
  • 最基本的示例之外的文档很少

原始 README

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

The Validator

A simple & lightweight form and data validation package for flutter

How to use

The Validator can be used directly on a TextFormField validator parameter TextFormField(validator: FieldValidator.email())

The Validator can also be used as a stand alone validator Validatror.isPassword('SecretPass', shouldContainCapitalLetter: true)

Sample Usage (FieldValidator)

TextFormField( 
    controller: _emailController,
    validator: FieldValidator.email(),
    decoration: InputDecoration(labelText: "Email"),
    ),
                
 TextFormField( 
    controller: _passwordController,
        validator: FieldValidator.password(
        minLength: 8,
        shouldContainNumber: true,
        shouldContainCapitalLetter: true,
        shouldContainSmallLetter: true,
        shouldContainSpecialChars: true,
        errorMessage: "Password must match the required format",
        isNumberNotPresent: () { return "Password must contain number"; },
        isSpecialCharsNotPresent: () { return "Password must contain special characters"; },
        isCapitalLetterNotPresent: () { return "Password must contain capital letters"; }
        ),
        decoration: InputDecoration(labelText: "Email"),
    ),

To validate confirm password field

  // To validate confirm password field
  TextFormField(
      controller: _confirmPasswordController,
      obscureText: true,
      validator: FieldValidator.equalTo(_passwordController, message: "Password Mismatch"),
      decoration: InputDecoration(labelText: "Confirm Password"),
  )

Then make a call to formState .validate() to perform the validations on the textfields


RaisedButton(
  onPressed: () {
    // validate() inputed data
    if (_formKey.currentState.validate()) {

          _scaffoldKey.currentState
          .showSnackBar(SnackBar(content: Text("Yay! we got it right!")));
                      
    }
  },
  child: Text("Submit"),                  
)

Sample Usage 2 (Validator)

var password = 'Secret'
var confirmPassword = 'secret'
if (Validator.isPassword(password, shouldContainCapitalLetter: true)) {
  if (Validator.isEqualTo(password, confirmPassword)) {
    print("Go ahead & grant access")
  } else {
    print("Password mis match")
  }
}

Error messages

By default The Validator will give you a nice default error based on the the type of validation you specify for field or an input. But you can also override the default message

Default Error Messages

FieldValidator.alphaNumeric() will output Field must contain both alphabets and numbers

User Defined Error Message

FieldValidator.alphaNumeric(message: 'Your username must contain both numbers & alphabets')

所属分类

源码仓库

使用此插件的项目

暂无相关数据。