Master Validator Help

Getting Started

Master validator is meant to be used when working with forms generally. The library provides a set of pre-written validator functions which you can give as a value to the validator argument of your text form field widget

Import master_validator package

import 'package:master_validator/master_validator.dart';

Usage

Basic Required Field Validation

Use Validators.Required() to make a field required.

TextFormField( validator: Validators.Required( errorMessage : "This field cannot be left empty", ) ),

Email Validation :

TextFormField( validator: Validators.Required( next : Validators.Email(), ), ),

Using Multiple Validators (Chaining Validators)

All validators take a next argument in which you can specify another validator (a custom function, or another predefined validator) like :

TextFormField( validator: Validators.Required( next : Validators.Email( next : Validators.Maxlength(50) ), ), ),

Finally

Finally, to validate your form, just call validate method from your form's key :

// supply the Form widget with the key Form( key : _formKey, child : Column(), )
// validate form inside some function if (_formKey.currentState!.validate()) { showSnackbar("Form is valid"); } else { showSnackbar("Form is invalid"); }

Extra

Master Validator by default also adds up the following extensions to String class :

  • isEmail

  • isInteger

  • isDouble

  • isValidDirectoryName

  • isValidURL

  • hasLengthBetween(min,max) All of these returns a boolean value which can be used as :

String email = "someone@org.com"; if(email.isEmail){ print("Valid Email"); }

For detailed usage, check example

Last modified: 05 January 2024