Skip to content

Commit

Permalink
Merge pull request #451 from artflutter/allowNull-flag-for-number-val…
Browse files Browse the repository at this point in the history
…idator

`allowNull` flag for number validator
  • Loading branch information
joanpablo authored May 10, 2024
2 parents 839968f + 0201a39 commit 30ba6d1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/src/validators/number_validator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class NumberValidator extends Validator<dynamic> {
/// decimal point in the validated string. Defaults to 0 (no decimals).
final int allowedDecimals;

/// Whether the validator allows null values.
final bool allowNull;

/// Whether the validator allows negative numbers.
///
/// If set to `true`, the validator will accept strings representing
Expand All @@ -27,12 +30,18 @@ class NumberValidator extends Validator<dynamic> {
/// [allowedDecimals] (optional): The allowed number of decimal places. Defaults to 0.
/// [allowNegatives] (optional): Whether to allow negative numbers. Defaults to true.
const NumberValidator({
this.allowNull = false,
this.allowedDecimals = 0,
this.allowNegatives = true,
}) : super();

@override
Map<String, dynamic>? validate(AbstractControl<dynamic> control) {
// Skip validation if null value is allowed
if (allowNull && control.value == null) {
return null;
}

if (control.value == null) {
return <String, dynamic>{
ValidationMessage.number: NumberValidatorError.nullValue,
Expand Down
2 changes: 2 additions & 0 deletions lib/src/validators/validators.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,12 @@ class Validators {
/// [allowedDecimals] (optional): The allowed number of decimal places. Defaults to 0.
/// [allowNegatives] (optional): Whether to allow negative numbers. Defaults to true.
static Validator<dynamic> number({
bool allowNull = false,
int allowedDecimals = 0,
bool allowNegatives = true,
}) =>
NumberValidator(
allowNull: allowNull,
allowedDecimals: allowedDecimals,
allowNegatives: allowNegatives,
);
Expand Down
19 changes: 19 additions & 0 deletions test/src/validators/number_validator_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,25 @@ void main() {
expect(control.hasError(ValidationMessage.number), true);
});

test('FormControl invalid if value is null', () {
final control = FormControl<String>(validators: [Validators.number()]);

expect(control.valid, false);
expect(control.hasError(ValidationMessage.number), true);
expect(control.errors, {
ValidationMessage.number: NumberValidatorError.nullValue,
});
});

test('FormControl valid if value is null and null values is allowed', () {
final control = FormControl<String>(
validators: [Validators.number(allowNull: true)],
);

expect(control.valid, true);
expect(control.hasError(ValidationMessage.number), false);
});

test('FormControl valid if a number', () {
final control = FormControl<String>(validators: [Validators.number()]);

Expand Down

0 comments on commit 30ba6d1

Please sign in to comment.