This package is intended to help create informative validation for flutter form fields.
The FormValidator
class is intended to be used to chain validation
functions together to produce one validator function as required by Widgets
such as TextFormField
. The final closure using the build method matches
the type required by the validator attribute for form fields "String? Function(String?)
".
To use this package add text_form_validator
as a package in your pub file.
import "package:text_form_validator/form_validator.dart";
// Basic, the field can't be null or empty
TextFormField(validator: FormValidator.required().build());
// All the messages which will be returned from validation can't be cusomtised
// as required. Each validator is run one after the other, so the message
// to the end user can be as specific as possible
TextFormField(validator: FormValidator
.notNull(nullError: "Woah enter something!")
.notEmpty()
.isNumeric()
.build());
// More complex validation can also be added on the fly
String customError = "Number must be a multiple of 100!";
ValidationFn validator = FormValidator
.notNull(nullError: "Woah enter something!")
.notEmpty()
.isNumeric()
// If it hits the custom we can assume the types will convert properly
.custom((p0) => (p0 == null || (num.parse(p0) % 100) != 0) ? customError : null)
.build();
TextFormField(validator: validator);
Below is a list of all current default functions provided, as specified these
are here to assist the user, and may not be exhaustive, see FormValidator.custom
for adding custom functions. The descriptions provided here are brief, check the
functions themselves in utils/string_validation_functions.dart
for full details.
notNull
: Not equal to nullnotEmpty
: Not equal to ""required
: CombinesnotNull
andnotEmpty
isNumeric
:num.tryParse
!= nullisDateTime
: The value can be formatted into a date (can specify custom format)equals
: == provided valuenotEquals
: != provided valuelengthGt
: value.length > providedValuelengthGtEq
: value.length >= providedValuelengthLt
: value.length < providedValuelengthLtEq
: value.length <= providedValuelengthEq
: value.length == providedValueinList
: The value is in the provided listnotInList
: The value is not in the provided listmatches
: The value matches the supplied regex patternisAnEmail
: The value matches theStringValidationFunctions.emailRegExp
pattern
The benefit of the chaining of functions allow user messages to be as customized as possible.
For StringValidationFunctions
errors are provided in line. This allows for
errors like in StringValidationFunctions.equals
where it specifies the equal
value in the default error.