FormValidator class
Validates form field input using a typed rule pipeline.
There are three patterns for building a validator:
- Named constructor — single rule:
FormValidator.notEmpty()
FormValidator.email()
- Chainable fluent API — multiple rules:
FormValidator().notEmpty().minLength(3)
FormValidator().email().nullable()
- Explicit list of FormRule objects:
FormValidator.rule([
FormRuleNotEmpty(null),
FormRuleMinLength(3, null),
])
Call .nullable() to skip all rules when the value is null or empty.
Passed to InputField.formValidator; the first failing rule's message is
shown beneath the field via InputDecoration.errorText.
Constructors
- FormValidator({dynamic data, String? attribute})
-
Create a new form validator with
messageanddata - FormValidator.beginsWith(String prefix, {String? message})
-
Validate that the value begins with
prefixmessageThe message to display if the value is invalid - FormValidator.booleanFalse({String? message})
-
Validate a value is a boolean
messageThe message to display if the value is invalid - FormValidator.booleanTrue({String? message})
-
Validate a value is a boolean
messageThe message to display if the value is invalid - FormValidator.capitalized({String? message})
-
Validate that the value is capitalized
messageThe message to display if the value is invalid -
FormValidator.contains(List<
String> values, {String? message}) -
Validate the value contains one of the
valuesmessageThe message to display if the value is invalid - FormValidator.custom({String? message, required bool validate(dynamic data)})
-
Validate a custom rule
messageThe message to display if the custom validation failsvalidateThe custom validation function that takes the data and returns a boolean - FormValidator.date({String? message})
-
Validate that the value is a date
messageThe message to display if the value is invalid - FormValidator.dateAgeIsOlder(int age, {String? message})
-
Validate that the date is older than
agemessageThe message to display if the value is invalidageThe age to compare - FormValidator.dateAgeIsYounger(int age, {String? message})
-
Validate that the date is younger than
agemessageThe message to display if the value is invalidageThe age to compare - FormValidator.dateInFuture({String? message})
-
Validate that the date is in the future
messageThe message to display if the value is invalid - FormValidator.dateInPast({String? message})
-
Validate that the date is in the past
messageThe message to display if the value is invalid - FormValidator.email({String? message})
-
Validate an email
messageThe message to display if the email is invalid - FormValidator.endsWith(String suffix, {String? message})
-
Validate that the value ends with
suffixmessageThe message to display if the value is invalid - FormValidator.lowercase({String? message})
-
Validate that the value is lowercase
messageThe message to display if the value is invalid - FormValidator.maxLength(int value, {String? message})
-
Validate that the value is a maximum of
valuecharactersmessageThe message to display if the value is invalidvalueThe maximum number of characters - FormValidator.maxSize(int value, {String? message})
-
Validate that the value is a maximum size of
valuemessageThe message to display if the value is invalidvalueThe maximum size - FormValidator.maxValue(int value, {String? message})
-
Validate that the value is a maximum of
valuemessageThe message to display if the value is invalidvalueThe maximum value - FormValidator.minLength(int value, {String? message})
-
Validate that the value is a minimum of
valuecharactersmessageThe message to display if the value is invalidvalueThe minimum number of characters - FormValidator.minSize(int value, {String? message})
-
Validate that the value is a minimum size of
valuemessageThe message to display if the value is invalidvalueThe minimum size - FormValidator.minValue(int value, {String? message})
-
Validate that the value is a minimum of
valuemessageThe message to display if the value is invalidvalueThe minimum value - FormValidator.notEmpty({String? message})
-
Validate that the value is not empty
messageThe message to display if the value is invalid - FormValidator.numeric({String? message})
-
Validate that the value is numeric
messageThe message to display if the value is invalid - FormValidator.password({int strength = 1, String? message})
-
Validate a password with a strength of 1 or 2
strength1: 1 uppercased letter, 1 digit, 8 charactersstrength2: 1 uppercased letter, 1 digit, 1 special character, 8 charactersmessageThe message to display if the password is invalid - FormValidator.phoneNumberUk({String? message})
-
Validate a UK phone number
messageThe message to display if the phone number is invalid - FormValidator.phoneNumberUs({String? message})
-
Validate a US phone number
messageThe message to display if the phone number is invalid - FormValidator.postcodeUk({String? message})
-
Validate that the value is a valid postcode for the UK
messageThe message to display if the value is invalid - FormValidator.regex(String regex, {String? message})
-
Validate that the value matches a
regexpatternmessageThe message to display if the value is invalid -
FormValidator.rule(List<
FormRule> rules, {dynamic data}) - Create a validator from an explicit list of FormRule objects.
- FormValidator.uppercase({String? message})
-
Validate that the value is uppercase
messageThe message to display if the value is invalid - FormValidator.url({String? message})
-
Validate a URL
messageThe message to display if the URL is invalid - FormValidator.zipcodeUs({String? message})
-
Validate that the value is a valid zipcode for the US
messageThe message to display if the value is invalid
Properties
Methods
-
beginsWith(
String prefix, {String? message}) → FormValidator -
Validate that the value begins with
prefix -
booleanFalse(
{String? message}) → FormValidator - Validate a value is a boolean (must be false)
-
booleanTrue(
{String? message}) → FormValidator - Validate a value is a boolean
-
capitalized(
{String? message}) → FormValidator - Validate that the value is capitalized
-
check(
[dynamic data]) → FormValidationResult - Check the validation of the data against the rules Returns a FormValidationResponse indicating success or error
-
contains(
List< String> values, {String? message}) → FormValidator -
Validate the value contains one of the
values -
custom(
{String? message, required bool validate(dynamic data)}) → FormValidator -
Validate a custom rule
messageThe message to display if the custom validation failsvalidateThe custom validation function that takes the data and returns a boolean -
date(
{String? message}) → FormValidator - Validate that the value is a date
-
dateAgeIsOlder(
int age, {String? message}) → FormValidator -
Validate that the date is older than
age -
dateAgeIsYounger(
int age, {String? message}) → FormValidator -
Validate that the date is younger than
age -
dateInFuture(
{String? message}) → FormValidator - Validate that the date is in the future
-
dateInPast(
{String? message}) → FormValidator - Validate that the date is in the past
-
email(
{String? message}) → FormValidator - Validate an email
-
endsWith(
String suffix, {String? message}) → FormValidator -
Validate that the value ends with
suffix -
equals(
dynamic data, {String? message}) → FormValidator - Validate that the value equals another value
-
lowercase(
{String? message}) → FormValidator - Validate that the value is lowercase
-
maxLength(
int value, {String? message}) → FormValidator -
Validate that the value is a maximum of
valuecharacters -
maxSize(
int value, {String? message}) → FormValidator -
Validate that the value is a maximum size of
value -
maxValue(
int value, {String? message}) → FormValidator -
Validate that the value is a maximum of
value -
minLength(
int value, {String? message}) → FormValidator -
Validate that the value is a minimum of
valuecharacters -
minSize(
int value, {String? message}) → FormValidator -
Validate that the value is a minimum size of
value -
minValue(
int value, {String? message}) → FormValidator -
Validate that the value is a minimum of
value -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
notEmpty(
{String? message}) → FormValidator - Validate that the value is not empty
-
nullable(
) → FormValidator - Mark this validator as nullable. When nullable, if the value is null or empty, validation will pass. If the value is not null/empty, all rules will be applied.
-
numeric(
{String? message}) → FormValidator - Validate that the value is numeric
-
password(
{required int strength, String? message}) → FormValidator - Validate that the value is a password
-
phoneNumberUk(
{String? message}) → FormValidator - Validate a UK phone number
-
phoneNumberUs(
{String? message}) → FormValidator - Validate a US phone number
-
postcodeUk(
{String? message}) → FormValidator - Validate that the value is a valid postcode for the UK
-
regex(
String regex, {String? message}) → FormValidator -
Validate that the value matches a
regexpattern -
setAttribute(
String? attribute) → void - Sets the attribute name for error messages.
-
setData(
dynamic data) → void - Set the data for the form validator
-
that(
dynamic data, [String? attribute]) → FormValidator -
Configures the validator with the value and attribute to validate, then
returns
thisso rules can be chained. -
toString(
) → String -
A string representation of this object.
inherited
-
uppercase(
{String? message}) → FormValidator - Validate that the value is uppercase
-
url(
{String? message}) → FormValidator - Validate a URL
-
zipcodeUs(
{String? message}) → FormValidator - Validate that the value is a valid zipcode for the US
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited