isNumberDoubleField static method
Validator to check if a field is a valid number within optional bounds.
max
: The maximum allowed value.min
: The minimum allowed value.isRequired
: Whether the field is required (non-null). Defaults tofalse
.
Implementation
static ValidatorEvent isNumberDoubleField({
double? max,
double? min,
bool isRequired = false,
}) {
return (value) {
var res = true;
var error = <String>[];
if (value != null) {
if (!value.toString().isDouble) {
res = false;
error.add('error.field.numeric');
} else {
if (max != null) {
if (value.toString().asDouble(def: 0) > max) {
res = false;
error.add('error.field.max#{$max}');
}
}
if (min != null) {
if (value.toString().asDouble(def: 0) < min) {
res = false;
error.add('error.field.min#{$min}');
}
}
}
} else if (isRequired) {
res = false;
error.add('error.field.required');
}
return FieldValidateResult(
success: res,
error: res ? '' : 'error.field',
errors: error,
);
};
}