MinLength static method
Returns a validator that checks if a string is at least a certain length.
Arguments :
length
: The minimum length of the string.errorMessage
: The error message to return if the string is not at least a certain length.next
: A validator to run after this validator.
Usage :
TextFormField(
validator: Validators.MinLength(length: 5),
),
Implementation
static String? Function(String? value)? MinLength({
required int length,
String errorMessage = 'Minimum length is *min_len*',
String? Function(String value)? next,
}) {
if (errorMessage.contains('*min_len*')) {
errorMessage = errorMessage.replaceAll('*min_len*', length.toString());
}
return (value) {
if (value == null || value.trim().isEmpty) {
return null;
}
if (value.length < length) {
return errorMessage;
}
if (next != null) {
return next(value);
}
return null;
};
}