wc_form_validators 0.1.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 80

Form Validators #

A Flutter plugin which provides a set of validators that can be used by form fields.

Build Status Unit Test Author

About #

This flutter plugin provides utility functions to make form validation easy. This plugin was inspired by Angular Validators class.

Version #

This plugin supports dart version 2.2+

How to use Form Validators #

For Example: #

Validating if TextFormField is non-empty and has a valid email address #

TextFormField(
  decoration: InputDecoration(
    labelText: 'Email',
  ),
  validator: Validators.compose([
    Validators.required('Email is required'),
    Validators.email('Invalid email address'),
  ]),
),

Api Overview #

Utility Functions #

All validator functions have return type of FormFieldValidator<String> which is a required type for validator field in TextFormField.


Required #

Validators.required(String errorMessage) is validator that requires the field have a non-empty value.

Example #

This code will validate the name and show error if it is empty.

TextFormField(
  decoration: InputDecoration(
    labelText: 'Name',
  ),
  validator: Validators.required('Name is required'),
),

Parameters #

ParamsDescription
errorMessageString value is passed to this parameter to show an error in case of a validation failure.

Minimum #

Validators.min(double min, String errorMessage) is a validator that requires the field's value be greater than or equal to the provided number (double min). TextFormField's value must be an integer or double otherwise this validator will throw a parse exception.

Example #

This code will validate a TextFormField's value and show an error in case its value is non-empty and less than 5.

  TextFormField(
    keyboardType: TextInputType.numberWithOptions(
      decimal: true,
      signed: true,
    ),
    decoration: InputDecoration(
      labelText: 'Minimum 5',
    ),
    validator: Validators.min(5, 'Value less than 5 not allowed'),
  ),

Parameters #

Validators.min takes two parameters.

ParamsDescription
mindouble value is passed to this param. Validator will return an error if TextFormField is non-empty and its value is less than min
errorMessageString value is passed to this parameter to show an error in case of a validation failure

Maximum #

Validators.max(double max, String errorMessage) is a validator that requires the field's value to be less than or equal to the provided number (double max). TextFormField's value must be an integer or double otherwise this validator will throw a parse exception.

Example #

This code will validate TextFormField's value and show an error in case its value is non-empty and greater than 5.

  TextFormField(
    keyboardType: TextInputType.numberWithOptions(
      decimal: true,
      signed: true,
    ),
    decoration: InputDecoration(
      labelText: 'Maximum 5',
    ),
    validator: Validators.max(5, 'Value greater than 5 not allowed'),
  ),

Parameters #

Validators.max takes two parameters.

ParamsDescription
maxdouble value is passed to this param. Validator will return an error if TextFormField is non-empty and its value is greater than max
errorMessageString value is passed to this parameter to show an error in case of validation failure

Email #

Validators.email(String errorMessage) is a validator that requires the field's value pass an email validation test.

Its uses regex of HTML5 for email validation. Its regex is ^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$

If you want to use custom regex for email validation, please take a look at Pattern.

Example #

This code will validate the email and show an error if TextFormField's value is non-empty and the email address is invalid.

  TextFormField(
    decoration: InputDecoration(
      labelText: 'Email',
    ),
    validator: Validators.email('Invalid email address'),
  ),

Parameters #

ParamsDescription
errorMessageString value is passed to this parameter to show an error in case of validation failure.

Minimum Length #

Validators.minLength(int minLength, String errorMessage) is a validator that requires the length of the field's value to be greater than or equal to the provided minimum length.

Example #

This code will validate the TextFormField's value and show an error in case field's value is non-empty and its character length is less than 5.

  TextFormField(
    decoration: InputDecoration(
      labelText: 'Minimum length 5',
    ),
    validator: Validators.minLength(5, 'Characters are less than 5'),
  ),

Parameters #

ParamsDescription
minLengthint value is passed to this param. Validator will return an error if TextFormField is non-empty and its number of characters is less than minLength.
errorMessageString value is passed to this parameter to show error in case of validation failure.

Maximum Length #

Validators.maxLength(int maxLength, String errorMessage) is a validator that requires the length of the field's value to be less than or equal to the provided maximum length.

Example #

This code will validate TextFormField's value and show an error in case the field's value is non-empty and its character length is greater than 5.

  TextFormField(
    decoration: InputDecoration(
      labelText: 'Maximum length 5',
    ),
    validator: Validators.maxLength(5, 'Characters are greater than 5'),
  ),

Parameters #

ParamsDescription
maxLengthint value is passed to this param. The validator will return an error if the TextFormField is non-empty and its number of characters are greater than maxLength.
errorMessageString value is passed to this parameter to show error in case of validation failure.

Pattern #

Validators.patternString(String pattern, String errorMessage) and Validators.patternRegExp(RegExp pattern, String errorMessage) are validators that require the field's value to match a regex pattern.

In case of Validators.patternString, you need to pass regex like this r"^[A-Za-z]+$"

Examples #

This code will validate the TextFormField's value and show an error in case the field's value is non-empty and it contains characters other than letters.

patternRegExp #
  TextFormField(
    decoration: InputDecoration(
      labelText: 'Pattern r"^[A-Za-z]+\$"',
    ),
    validator: Validators.patternRegExp(
        RegExp(r"^[A-Za-z]+$"), 'Only alphabets are allowed'),
  ),

patternString #
  TextFormField(
    decoration: InputDecoration(
      labelText: 'Pattern r"^[A-Za-z]+\$"',
    ),
    validator: Validators.patternString(
        r"^[A-Za-z]+$", 'Only alphabets are allowed'),
  ),

Parameters #

ParamsDescription
patternString or RegExp the value is passed to this param. The validator will return an error if the TextFormField is non-empty and it's not matching the pattern that you have provided.
errorMessageString value is passed to this parameter to show error in case of a validation failure.

Compose #

Validators.compose(List<FormFieldValidator<String>> validators) composes multiple validators into a single validator.

How it works #

This function takes multiple validators as a list and will validate each validator in the provided sequence. If any of its validator returns an error message, it won't check the remaining validators and will return this error to the TextFormField.

Example #

This code will validate that Name is non-empty and has a character length between 5 and 10.

  TextFormField(
    decoration: InputDecoration(
      labelText: 'Name',
    ),
    validator: Validators.compose([
      Validators.required('Name is required'),
      Validators.minLength(5, 'Name cannot be less than 5 characters'),
      Validators.maxLength(10, 'Name cannot be greater than 10 characters'),
    ]),
  ),

Parameters #

ParamsDescription
validatorsList<FormFieldValidator<String>> value is passed to this param containing validators that you want to combine into one validator

Note: #

If TextFormField's value is empty, then validators Minimum, Maximum, Email, Minimum Length, Maximum Length and Pattern won't return any error because it considers TextFormField as optional. Use these validators in combination with the Required validator if the specified TextFormField is compulsory and you want a validation failure if the field is empty. Check Compose validator to find out how to merge multiple validators.


[0.1.1]

  • Documentation updated
  • License added

[0.1.0]

  • Initial release

example/README.md

Examples #

Utility Functions #

Required #

lib/required_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        decoration: InputDecoration(
          labelText: 'Required',
        ),
        validator: Validators.required('Field is required'),
      ),
      RaisedButton(
        child: Text('Validate Required Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Minimum #

lib/min_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        keyboardType: TextInputType.numberWithOptions(
          decimal: true,
          signed: true,
        ),
        decoration: InputDecoration(
          labelText: 'Minimum 5',
        ),
        validator: Validators.min(5, 'Value less than 5 not allowed'),
      ),
      RaisedButton(
        child: Text('Validate Minimum 5 Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Maximum #

lib/max_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        keyboardType: TextInputType.numberWithOptions(
          decimal: true,
          signed: true,
        ),
        decoration: InputDecoration(
          labelText: 'Maximum 5',
        ),
        validator: Validators.max(5, 'Value greater than 5 not allowed'),
      ),
      RaisedButton(
        child: Text('Validate Maximum 5 Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Email #

lib/email_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        decoration: InputDecoration(
          labelText: 'Email',
        ),
        validator: Validators.email('Invalid email address'),
      ),
      RaisedButton(
        child: Text('Validate email Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Minimum Length #

lib/min_length_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        decoration: InputDecoration(
          labelText: 'Minimum length 5',
        ),
        validator: Validators.minLength(5, 'Characters are less than 5'),
      ),
      RaisedButton(
        child: Text('Validate Minimum length 5 Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Maximum Length #

lib/max_length_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        decoration: InputDecoration(
          labelText: 'Maximum length 5',
        ),
        validator: Validators.maxLength(5, 'Characters are greater than 5'),
      ),
      RaisedButton(
        child: Text('Validate Maximum length 5 Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Pattern #

lib/pattern_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        decoration: InputDecoration(
          labelText: 'Pattern r"^[A-Za-z]+\$"',
        ),
        validator: Validators.patternRegExp(
            RegExp(r"^[A-Za-z]+$"), 'Only alphabets are allowed'),
      ),
      RaisedButton(
        child: Text('Validate Pattern Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Compose #

lib/compose_validation_container.dart #
Form(
  key: formKey,
  child: Column(
    children: [
      TextFormField(
        decoration: InputDecoration(
          labelText: 'Compose',
        ),
        validator: Validators.compose([
          Validators.required('Compose is required'),
          Validators.minLength(5, 'Characters cannot be less than 5'),
          Validators.maxLength(10, 'Characters cannot be greater than 10'),
        ]),
      ),
      RaisedButton(
        child: Text('Validate compose Field'),
        onPressed: () {
          formKey.currentState.validate();
        },
      )
    ],
  ),
);

Use this package as a library

1. Depend on it

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


dependencies:
  wc_form_validators: ^0.1.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support 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:wc_form_validators/wc_form_validators.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
62
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
95
Overall:
Weighted score of the above. [more]
80
Learn more about scoring.

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

  • Dart: 2.6.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

The package description is too short. (-5 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test