0 purchases
bro form validator
Bro Form Validator #
Form validator that provide common and custom validators for brovelopers
Usage #
assign it directly to a TextFormField validator
TextFormField(
validator: EmailValidator(errorText: 'enter a valid email address')
);
copied to clipboard
is possible to create an instance to reuse it
final requiredValidator = RequiredValidator(errorText: 'this field is required');
copied to clipboard
Multi rules validation #
assign the the multi validator to the TextFormField validator
final passwordValidator = MultiValidator([
RequiredValidator(errorText: 'password is required'),
MinLengthValidator(8, errorText: 'password must be at least 8 digits long'),
PatternValidator(r'(?=.*?[#?!@$%^&*-])', errorText: 'passwords must have at least one special character')
]);
TextFormField(
validator: passwordValidator,
),
copied to clipboard
It's possible to nest MultiValidator
final passwordValidator = MultiValidator([
MultiValidator([
RequiredValidator(errorText: 'password is required'),
MinLengthValidator(8, errorText: 'password must be at least 8 digits long'),
]),
PatternValidator(r'(?=.*?[#?!@$%^&*-])', errorText: 'passwords must have at least one special character')
]);
copied to clipboard
Make your own validator #
use BroFieldValidator
class PhoneValidator extends BroFieldValidator {
// pass the error text to the super constructor
PhoneValidator({String errorText = 'enter a valid LYD phone number'}) : super(errorText);
// return false if you want the validator to return error
// message when the value is empty.
@override
bool get ignoreEmptyValues => true;
@override
bool isValid(String value) {
// return true if the value is valid according the your condition
return hasMatch(r'^((+|00)?218|0?)?(9[0-9]{8})$', value);
}
}
copied to clipboard
and use this directly
TextFormField(validator: PhoneValidator());
copied to clipboard
Make your own validator with non String values #
just extend the base FieldValidator class
class CustomValidator extends FieldValidator<T>{
CustomValidator(String errorText) : super(errorText);
@override
bool isValid(T value) {
return //condition;
}
}
copied to clipboard
For personal and professional use. You cannot resell or redistribute these repositories in their original state.
There are no reviews.