flrx_validator 0.3.0

Flrx Validator #

It is a fluent API based FormField validator for flutter, that promotes code reuse. It is highly extensible, modular and testable.

Pub Build Status codecov GitHub

Installation #

Add the following to your pubspec.yaml file

dependencies:
  flrx_validator: ^0.3.0

Usage #

Documentation #

For more info on Flrx Validator and Rules you can see the documentation.

Built In Rules #

RuleArgumentsDescription
AnyRuleruleListTakes A list of rules, Passes when any of the rule passes, Useful for Validation of Username/Email Fields or similar fields
EmailRule-Passes when String is an E-Mail
InRuleacceptedListPasses when the value is included in the given list of values.
MaxLengthRulemaxLengthPasses when the String length is less than the given length
MinLengthRuleminLengthPasses when the String length is more than the given length
NotInRulerejectedListPasses when the value is not included in the given list of values.
RegexRuleregexPasses when the value matches the given Regex.
RequiredRule-Passes when the value is not null or empty.

Can't see a rule you need? Raise an issue or create a Pull Request.

Example #

import 'package:flrx_validator/validator.dart';
import 'package:flrx_validator/rules/email_rule.dart';
import 'package:flrx_validator/rules/required_rule.dart';

....
....

TextFormField(
    validator: Validator<String>()
        .add(RequiredRule())
        .add(EmailRule())
        .build()
);

....
....

Contributing #

Please see CONTRIBUTING.

License #

Please see LICENSE.

[v0.3.0]

Added #

  • InRule, NotInRule

Deprecated #

  • OneOfRule in favour of InRule

Changed #

  • Minor Code Cleanup.

v0.2.0 #

Added #

  • Support for all Dart projects.

v0.1.0 #

Initial Release

example/README.md

Validator Examples #

Using FormField #

example/lib/material_form.dart #

import 'package:flrx_validator/rules/email_rule.dart';
import 'package:flrx_validator/rules/required_rule.dart';
import 'package:flrx_validator/validator.dart';
import 'package:flutter/material.dart';

class MaterialForm extends StatefulWidget {
  MaterialForm({Key key}) : super(key: key);

  @override
  _MaterialFormState createState() => _MaterialFormState();
}

class _MaterialFormState extends State<MaterialForm> {
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Material Form Validator')),
      body: SingleChildScrollView(
        padding: const EdgeInsets.all(16),
        child: Form(
          key: _formKey,
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              TextFormField(
                validator:
                    Validator().add(RequiredRule()).add(EmailRule()).build(),
                decoration: InputDecoration(hintText: 'Email'),
              ),
              buildDropdown(),
              Padding(
                padding: const EdgeInsets.symmetric(vertical: 16.0),
                child: RaisedButton(
                  onPressed: onFormSubmitPressed,
                  child: Text('Submit'),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget buildDropdown() {
    return DropdownButtonFormField<String>(
      validator: Validator().add(RequiredRule()).build(),
      value: "",
      items: <DropdownMenuItem<String>>[
        DropdownMenuItem<String>(
          child: Text('Please select an Item'),
          value: "",
        ),
        DropdownMenuItem<String>(
          child: Text('Item 1'),
          value: "Item 1",
        )
      ],
    );
  }

  void onFormSubmitPressed() {
    if (_formKey.currentState.validate()) {
      // Process data.
    }
  }
}

Using TextEditingController #

example/lib/cupertino_form.dart #

import 'package:flrx_validator/rules/email_rule.dart';
import 'package:flrx_validator/rules/required_rule.dart';
import 'package:flrx_validator/validator.dart';
import 'package:flutter/cupertino.dart';

class CupertinoForm extends StatefulWidget {
  @override
  _CupertinoFormState createState() => _CupertinoFormState();
}

class _CupertinoFormState extends State<CupertinoForm> {
  TextEditingController _textEditingController = TextEditingController();
  String emailValidationMessage = "";

  @override
  Widget build(BuildContext context) {
    return DefaultTextStyle(
      style: const TextStyle(
        fontFamily: '.SF UI Text',
        inherit: false,
        fontSize: 17.0,
        color: CupertinoColors.black,
      ),
      child: CupertinoPageScaffold(
          navigationBar: CupertinoNavigationBar(
            previousPageTitle: "Form Selection",
            middle: Text('Cupertino Form Validation'),
          ),
          child: CupertinoScrollbar(
            child: SingleChildScrollView(
              padding:
                  const EdgeInsets.symmetric(vertical: 32.0, horizontal: 16.0),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: <Widget>[
                  SizedBox(height: 72),
                  CupertinoTextField(
                    controller: _textEditingController,
                    prefixMode: OverlayVisibilityMode.always,
                    placeholder: 'Email',
                    prefix: Icon(
                      CupertinoIcons.mail_solid,
                      color: CupertinoColors.lightBackgroundGray,
                      size: 28.0,
                    ),
                    decoration: BoxDecoration(
                      border: Border(
                          bottom: BorderSide(
                              width: 0.0, color: CupertinoColors.inactiveGray)),
                    ),
                    padding:
                        EdgeInsets.symmetric(horizontal: 6.0, vertical: 12.0),
                    clearButtonMode: OverlayVisibilityMode.editing,
                  ),
                  Padding(
                    padding: const EdgeInsets.fromLTRB(34, 8, 16, 16),
                    child: Text(
                      emailValidationMessage,
                      style: TextStyle(
                        color: CupertinoColors.destructiveRed,
                        fontSize: 12,
                      ),
                    ),
                  ),
                  Center(
                    child: CupertinoButton.filled(
                        child: Text('Submit'), onPressed: onFormSubmitPressed),
                  )
                ],
              ),
            ),
          )),
    );
  }

  @override
  void dispose() {
    _textEditingController.dispose();
    super.dispose();
  }

  void onFormSubmitPressed() {
    String value = _textEditingController.text;
    setState(() {
      emailValidationMessage = Validator<String>()
              .add(RequiredRule())
              .add(EmailRule())
              .validate(value) ??
          '';
    });
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  flrx_validator: ^0.3.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:flrx_validator/rule.dart';
import 'package:flrx_validator/rules/any_rule.dart';
import 'package:flrx_validator/rules/email_rule.dart';
import 'package:flrx_validator/rules/in_rule.dart';
import 'package:flrx_validator/rules/max_length_rule.dart';
import 'package:flrx_validator/rules/min_length_rule.dart';
import 'package:flrx_validator/rules/not_in_rule.dart';
import 'package:flrx_validator/rules/one_of_rule.dart';
import 'package:flrx_validator/rules/regex_rule.dart';
import 'package:flrx_validator/rules/required_rule.dart';
import 'package:flrx_validator/validator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
26
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]
63
Learn more about scoring.

We analyzed this package on Aug 25, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in libraries.

Health suggestions

Format lib/rules/email_rule.dart.

Run dartfmt to format lib/rules/email_rule.dart.

Format lib/rules/one_of_rule.dart.

Run dartfmt to format lib/rules/one_of_rule.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
meta ^1.1.6 1.1.7
Dev dependencies
pedantic ^1.8.0
test ^1.6.5
test_coverage ^0.3.0+1