Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NumberValidator(allowedDecimals) not working as intended #466

Open
cledoux95 opened this issue Sep 30, 2024 · 0 comments
Open

NumberValidator(allowedDecimals) not working as intended #466

cledoux95 opened this issue Sep 30, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@cledoux95
Copy link

cledoux95 commented Sep 30, 2024

Environment

17.0.1

No Flutter Doctor output here, just a runtime bug when attempting to use the NumberValidator

Code sample I'm using reactive_forms_generator, but the principle is the same:

Model:

@RfGroup
class MyClass {
  double? price; // Signed decimal where I want to enforce a maximum length
  
  MyClass({
    @RfControl(
        validators: [NumberValidator(allowedDecimals: 2)])
    this.price,
  });
}

UI Code:

ReactiveTextField<double>(
    formControl: formModel.MyClassForm.priceControl,
    validationMessages: {
      ValidationMessage.number: (_) =>
          'Cannot have more than 2 decimals',
    },
    valueAccessor: DoubleValueAccessor(),
    keyboardType: const TextInputType
        .numberWithOptions(decimal: true),
    decoration: InputDecoration(
        labelText: 'Price',
  ),

Description

Expected behavior:
According to the docs within the NumberValidator:
reactive_forms-17.0.1/lib/src/validators/number_validator.dart:

...
  /// The allowed number of decimal places in the validated string.
  ///
  /// This value **specifies the maximum number of digits allowed** after the
  /// decimal point in the validated string. Defaults to 0 (no decimals).
  final int allowedDecimals;
...

Current behavior:

In the same file, line 107 only allows for exactly that number of decimals, not up to the maximum, as stated in the comments within the file:

    // Check if the decimal part length is equal to the allowed decimals
    return parts[1].length == allowedDecimals; // <-- Should be '<='

Steps to reproduce

Utilize the NumberValidator with allowedDecimals set to any number greater than 1.

Images

Working text input:
Screen Shot 2024-09-29 at 9 23 57 PM

Working with exactly two decimal places
Screen Shot 2024-09-29 at 9 24 11 PM

Not working with less than two decimal places
Screen Shot 2024-09-29 at 9 24 20 PM

@cledoux95 cledoux95 added the bug Something isn't working label Sep 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant