flutter_validator_pro 0.1.0
flutter_validator_pro: ^0.1.0 copied to clipboard
A Flutter plugin for form validation including email, password, phone number, and custom multi-validator support.
validators #
A lightweight Flutter package for simple, chainable form validation.
Right now the package focuses on email validation through a fluent API, so you can stack rules and return the first validation error as a String?.
Features #
- Chain multiple email validation rules in a readable way
- Return
nullwhen validation passes - Return the first validation message when validation fails
- Keep validation logic outside widgets and forms
Installation #
Add the package to your pubspec.yaml:
dependencies:
validators:
path: ../validators
Then run:
flutter pub get
Import #
import 'package:validators/validators.dart';
Basic Usage #
Use Validatiors to start a validation chain and call validate() at the end.
final error = Validatiors()
.emailValidator(value: 'bharat.sc01@gmail.com')
.isRequired()
.isValidEmail()
.noSpaces()
.validate();
if (error == null) {
print('Email is valid');
} else {
print(error);
}
This works well with Flutter form fields:
TextFormField(
validator: (value) => Validatiors()
.emailValidator(value: value)
.isRequired()
.isValidEmail()
.noSpaces()
.singleAtSymbol()
.noConsecutiveDots()
.validate(),
)
Available Email Rules #
The current email validator supports these chainable methods:
isRequired()isValidEmail()noSpaces()hasAtSymbol()allowDomain(List<String> domains)minLength(int length)maxLength(int length)singleAtSymbol()noStartingDot()noEndingDot()noConsecutiveDots()validate()
Examples #
Validate that an email includes @:
final error = Validatiors()
.emailValidator(value: 'bharat.sc01.com')
.hasAtSymbol()
.validate();
Restrict allowed domains:
final error = Validatiors()
.emailValidator(value: 'user@company.com')
.allowDomain(['.com', '.in', '.outlook'])
.validate();
Apply multiple checks together:
final error = Validatiors()
.emailValidator(value: ' user..name@gmail.com ')
.isRequired()
.noSpaces()
.singleAtSymbol()
.noStartingDot()
.noEndingDot()
.noConsecutiveDots()
.validate();
How Validation Works #
Each rule checks the current value and stores the first error it finds. Once an error has been set, later rules do not replace it. validate() returns:
nullwhen the value passes all applied rules- An error message when a rule fails
Current Scope #
The public API currently provides:
- Email validation support through
emailValidator(...) - A placeholder
contactValidator(...)method that is not yet implemented with validation rules
Testing #
Run the package tests with:
flutter test
Notes #
- The entry class is named
Validatiorsin the current implementation. emailValidatorexpects a non-null value internally, so it is safest to use it with form values that are present or passed directly from a validator callback.