A Flutter plugin which provides a set of validators that can be used by form fields.
This flutter plugin provides utility functions to make form validation easy. This plugin is inspiration from Angular Validators class.
This plugin supports dart version 2.2+
TextFormField(
decoration: InputDecoration(
labelText: 'Email',
),
validator: Validators.compose([
Validators.required('Email is required'),
Validators.email('Invalid email address'),
]),
),
All validator functions have have return type of FormFieldValidator<String>
which is required type for validator
field in TextFormField
.
Validators.required(String errorMessage)
is validator that requires the field have a non-empty value.
This code will validate if name and show error if it is empty.
TextFormField(
decoration: InputDecoration(
labelText: 'Name',
),
validator: Validators.required('Name is required'),
),
Params | Description |
---|---|
errorMessage | String value is passed to this parameter to show an error in case of validation failure. |
Validators.min(double min, String errorMessage)
is a validator that requires the field's value to be greater than or equal to the provided number (double min
). TextFormField's value must be integer or double otherwise this validator will throw parse exception.
This code will validate TextFormField's value and show an error in case its value is non-empty and less than 5.
TextFormField(
keyboardType: TextInputType.numberWithOptions(
decimal: true,
signed: true,
),
decoration: InputDecoration(
labelText: 'Minimum 5',
),
validator: Validators.min(5, 'Value less than 5 not allowed'),
),
Validators.min
takes two parameters.
Params | Description |
---|---|
min | double value is passed to this param. Validator will return error if TextFormField is non-empty and its value is less than min |
errorMessage | String value is passed to this parameter to show error in case of validation failure |
Validators.max(double max, String errorMessage)
is a validator that requires the field's value to be less than or equal to the provided number (double max
). TextFormField's value must be integer or double otherwise this validator will throw parse exception.
This code will validate TextFormField's value and show error in case its value is non-empty and greater than 5.
TextFormField(
keyboardType: TextInputType.numberWithOptions(
decimal: true,
signed: true,
),
decoration: InputDecoration(
labelText: 'Maximum 5',
),
validator: Validators.max(5, 'Value greater than 5 not allowed'),
),
Validators.max
takes two parameters.
Params | Description |
---|---|
max | double value is passed to this param. Validator will return error if TextFormField is non-empty and its value is greater than max |
errorMessage | String value is passed to this parameter to show error in case of validation failure |
Validators.email(String errorMessage)
is a validator that requires the field's value pass an email validation test.
Its uses regex of HTML5 for email validation.
Its regex is ^[a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+@[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,253}[a-zA-Z0-9])?)*$
If you want to use custom regex for email validation, please take a look at Pattern.
This code will validate email and show error if TextFormField's value is non-empty and email address is invalid.
TextFormField(
decoration: InputDecoration(
labelText: 'Email',
),
validator: Validators.email('Invalid email address'),
),
Params | Description |
---|---|
errorMessage | String value is passed to this parameter to show an error in case of validation failure. |
Validators.minLength(int minLength, String errorMessage)
is a validator that requires the length of the field's value to be greater than or equal to the provided minimum length.
This code will validate TextFormField's value and show error in case of field's value is non-empty and its character's length is less than 5.
TextFormField(
decoration: InputDecoration(
labelText: 'Minimum length 5',
),
validator: Validators.minLength(5, 'Characters are less than 5'),
),
Params | Description |
---|---|
minLength | int value is passed to this param. Validator will return error if TextFormField is non-empty and its number of characters are less than minLength . |
errorMessage | String value is passed to this parameter to show error in case of validation failure. |
Validators.maxLength(int maxLength, String errorMessage)
is a validator that requires the length of the field's value to be less than or equal to the provided maximum length.
This code will validate TextFormField's value and show error in case of field's value is non-empty and its character's length is greater than 5.
TextFormField(
decoration: InputDecoration(
labelText: 'Maximum length 5',
),
validator: Validators.maxLength(5, 'Characters are greater than 5'),
),
Params | Description |
---|---|
maxLength | int value is passed to this param. Validator will return error if TextFormField is non-empty and its number of characters are greater than maxLength . |
errorMessage | String value is passed to this parameter to show error in case of validation failure. |
Validators.patternString(String pattern, String errorMessage)
and Validators.patternRegExp(RegExp pattern, String errorMessage)
are validators that requires the field's value to match a regex pattern.
In case of Validators.patternString
, you need to pass regex like this r"^[A-Za-z]+$"
This code will validate TextFormField's value and show error in case of field's value is non-empty and its contains character other than alphabets.
TextFormField(
decoration: InputDecoration(
labelText: 'Pattern r"^[A-Za-z]+\$"',
),
validator: Validators.patternRegExp(
RegExp(r"^[A-Za-z]+$"), 'Only alphabets are allowed'),
),
TextFormField(
decoration: InputDecoration(
labelText: 'Pattern r"^[A-Za-z]+\$"',
),
validator: Validators.patternString(
r"^[A-Za-z]+$", 'Only alphabets are allowed'),
),
Params | Description |
---|---|
pattern | String or RegExp value is passed to this param. Validator will return error if TextFormField is non-empty and its not matching pattern that you have provided. |
errorMessage | String value is passed to this parameter to show error in case of validation failure. |
Validators.compose(List<FormFieldValidator<String>> validators)
composes multiple validators into a single validator.
This function takes multiple validators as list and will validate each validator in provided sequence. If any of its validator returns error message, it won't check remaining validators and will return this error to TextFormField
.
This code will validate that Name is non-empty and has character length between 5 and 10.
TextFormField(
decoration: InputDecoration(
labelText: 'Name',
),
validator: Validators.compose([
Validators.required('Name is required'),
Validators.minLength(5, 'Name cannot be less than 5 characters'),
Validators.maxLength(10, 'Name cannot be greater than 10 characters'),
]),
),
Params | Description |
---|---|
validators | List<FormFieldValidator<String>> value is passed to this param containing validators that you want to combine into one validator |
If
TextFormField
's value is empty, then validators Minimum, Maximum, Email, Minimum Length, Maximum Length and Pattern won't return any error because it considersTextFormField
as optional. Use these validators with combination of Required validator if specifiedTextFormField
is compulsory and want validation failure if field is empty. Check Compose validator to find out how to merge multiple validators.