flrx_validator 0.4.0+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 70

Flrx Validator #

Pub Build Status codecov GitHub

A fluent API based validator designed around flutter that promotes code reuse. It is highly extensible, modular and testable.

Validator

Installation #

Add the following to your pubspec.yaml file

dependencies:
  flrx_validator: ^0.4.0+2

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
EachRuleruleListTakes A list of rules, Passes when all of the rule passes and returns validation message for all rules, Useful for Validation of Password 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/flrx_validator';
import 'package:flrx_validator/rules/email_rule.dart';
import 'package:flrx_validator/rules/required_rule.dart';

....
....

TextFormField(
    validator: Validator<String>(
      rules: [RequiredRule(), EmailRule()]
    )
);

....
....

Contributing #

Please see CONTRIBUTING.

License #

Please see LICENSE.

v0.4.0+2 #

Added #

  • EachRule
  • Made Validator a callable class
  • Allow adding rules via Validator constructor

Deprecated #

  • Validator.build()

Changed #

  • Moved all files to src folder. Use import 'package:flrx_validator/flrx_validator.dart'; BREAKING CHANGE
  • Minor Code cleanup

Fixed #

  • Fixed transformMessage not applying properly in AnyRule

Removed #

  • OneOfRule (deprecated in v0.3.0)

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/flrx_validator';
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()),
                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()),
      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/flrx_validator';
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.4.0+2

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/flrx_validator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
40
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]
70
Learn more about scoring.

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

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:flrx_validator/flrx_validator.dart.

Dependencies

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