isNumberField static method

ValidatorEvent isNumberField({
  1. int? max,
  2. int? min,
  3. bool isRequired = false,
})

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 to false.

Implementation

static ValidatorEvent isNumberField({
  int? max,
  int? min,
  bool isRequired = false,
}) {
  return (value) {
    var res = true;
    var error = <String>[];

    if (value != null) {
      if (!value.toString().isInt) {
        res = false;
        error.add('error.field.numeric');
      } else {
        if (max != null) {
          if (value.toString().toInt() > max) {
            res = false;
            error.add('error.field.max#{$max}');
          }
        }

        if (min != null) {
          if (value.toString().toInt() < 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,
    );
  };
}