validate method
This validator type supports asynchronous validation
It might come useful e.g. if you need to make some backend validation of
the data entered into your form. With this validator
you don't have to invent a custom approach to validate anything. Just
do all of your asynchronous work inside the validator's body and
then return the result.
returns
Returns an error text if the validation fails.
If it returns null then the validation is successful
Implementation
@override
FutureOr<String?> validate(
Object? value, {
String? fieldName,
}) {
if (!_isRequired) {
return null;
}
if (value == null) {
return errorText ?? '$fieldName is required';
}
if (value is DateTime) {
final difference = AgeDifference.fromNow(value);
final diffMonth = difference.months;
final diffYears = difference.years;
if (maxAgeYears != null) {
if (diffYears > maxAgeYears!) {
return errorText ?? 'You must not be older than $maxAgeYears years';
}
}
if (minAgeYears != null) {
if (diffYears < minAgeYears!) {
return errorText ?? 'You must be at least $minAgeYears years old';
}
}
if (maxAgeMonths != null) {
if (diffMonth > maxAgeMonths!) {
return errorText ?? 'You must not be older than $maxAgeMonths months';
}
}
if (minAgeMonths != null) {
if (diffMonth < minAgeMonths!) {
return errorText ?? 'You must be at least $minAgeMonths months old';
}
}
}
return null;
}