reactive_forms icon indicating copy to clipboard operation
reactive_forms copied to clipboard

distinguish validation for number (num) and integer (int)

Open Ch4rl3B opened this issue 1 year ago • 0 comments

Actually the Validator.number is validating integer values:

 /// Gets a validator that validates if the control's value is a numeric value.
static Validator<dynamic> get number => const NumberValidator();

...

/// Validator that validates if control's value is a numeric value.
class NumberValidator extends Validator<dynamic> {
  /// The regex expression of a numeric string value.
  static final RegExp numberRegex = RegExp(r'^-?[0-9]+$');

  const NumberValidator() : super();

  @override
  Map<String, dynamic>? validate(AbstractControl<dynamic> control) {
    return (control.value == null) ||
            !numberRegex.hasMatch(control.value.toString())
        ? <String, dynamic>{ValidationMessage.number: true}
        : null;
  }
}

could it be possible to validate numeric values accepting floating values and then have a Validator.integer for integer values?

Ch4rl3B avatar Nov 29 '23 06:11 Ch4rl3B