formdator 0.7.1 formdator: ^0.7.1 copied to clipboard
A formidable collection of Flutter form field validators that can be selected and grouped into various combinations through composition — Decorator Pattern.
formdator #
Formidable Validator — Formdator is a fully object-oriented package for validating Flutter form fields. Its key benefits, compared to all other similar packages, include:
- Object-oriented mindset: there is no static functions, only trustworthy immutable objects.
- Classes with short — yet meaningful — names like
Req
for a required (non-blank) field;ReqEmail
for a non-blank and well-formed email;Len.min
for a minimum number of characters; and so on. - Easy-to-compose validators, e.g.,
Trim(Email())
produces a validator that trims the entered email before validating it. - Contains a built-in set of ready-to-use compound validators: if you need to
validate an email and limit its length to, say, 50 chars, simply pass an
Email.len(50)
orReqEmail.len(50)
object as the validation argument. - You can apply multiple validation rules at once by using the
Pair
orRules
classes.
For easier integration with Flutter form fields, each validator implements the
call()
method so that any validator object can be called like a function —
Callable classes.
Getting Started #
A flexible package provides components that can be selected and grouped in various combinations so that user requirements can be fulfilled.
The code below shows how you can easily group the Rules
, Req
, Len
, and
Email
validators to form a 'trimmed-required-max-50-chars-email' constraint.
@override
Widget build(BuildContext context) {
return TextFormField(
onSaved: _onSaved,
validator: Rules<String>([
Req(),
Len.max(50),
Email(),
]),
keyboardType: TextInputType.emailAddress,
);
}
Or — even better — if the compound validator ReqEmail
is used to perform the
same task.
@override
Widget build(BuildContext context) {
return TextFormField(
onSaved: _onSaved,
validator: ReqEmail.len(50),
keyboardType: TextInputType.emailAddress,
);
}
The shorter command ReqEmail.len(50)
is equivalent to the much longer command
Rules<String>([Req(), Len.max(50), Email()])
.
Demo application #
The demo application provides a fully working example, focused on demonstrating exactly four validators in action — Pair, ReqLen, ReqEmail, and Equal. You can take the code in this demo and experiment with it.
To run the demo application:
git clone https://github.com/dartoos-dev/formdator.git
cd formdator/example/
flutter run -d chrome
This should launch the demo application on Chrome in debug mode.