A very simple package to help you write robust, reusable and extendable validations to go with your Flutter Forms.
Create simple or complex blocks of validation, and compose those building blocks to create flutter validators. Each block will have it's own logic, a set of args to create dynamic validators & specific error messages for each block.
There are simple ones, configurable ones and even you can create validations aligned to the domain of the app.
A warden will be a collection of validation blocks. Examples of enum & email pattern checking is added below.
To use this package, add form_warden
as a dependency in your pubspec.yaml file.
import 'package:form_warden/form_warden.dart';
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: createWarden([Validators.required]),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: f.label,
),
onSaved: null,
),
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: createWarden([Validators.required, Validators.email]),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: f.label,
),
onSaved: null,
),
ValidatorFunction greaterThanTen = (value) {
if (value is int && value > 10) {
return null;
}
return "Must be greater than 10";
};
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: createWarden([greaterThanTen]),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: f.label,
),
onSaved: null,
)
ValidatorFunction greaterThanTen = (value) {
if (value is int && value > 10) {
return null;
}
return "Must be greater than 10";
};
ValidatorFunction lessThanHundred = (value) {
if (value is int && value < 100) {
return null;
}
return "Must be less than 100";
};
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: createWarden([greaterThanTen, lessThanHundred]),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: f.label,
),
onSaved: null,
)
ValidatorFunction between(int lowerLimit, int upperLimit) {
ValidatorFunction greaterThanLowerLimit = (dynamic? value) {
if (value.isEmpty) return null;
var _v = int.parse(value);
if (_v > lowerLimit && _v < upperLimit) return null;
return "Value must be between $lowerLimit and $upperLimit";
};
return greaterThanLowerLimit;
}
TextFormField(
autovalidateMode: AutovalidateMode.onUserInteraction,
validator: createWarden([between(10, 100)]),
decoration: InputDecoration(
border: OutlineInputBorder(),
labelText: f.label,
),
onSaved: null,
)