Skip to content

Commit

Permalink
NumericRangeValidator: AllowNull (#1686)
Browse files Browse the repository at this point in the history
* AllowNull range validator

* Change comment

* Add AllowNull test case

* Fix test assertion

* Update RadzenNumericRangeValidator.cs
  • Loading branch information
monsieurvor authored Sep 11, 2024
1 parent 0566244 commit 3561062
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
13 changes: 13 additions & 0 deletions Radzen.Blazor.Tests/NumericRangeValidatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ public void Returns_False_If_Value_Is_Null()

Assert.False(component.Instance.Validate(null));
}
[Fact]
public void Returns_True_If_Value_Is_Null_And_AllowNull_Is_True()
{
using var ctx = new TestContext();
var component = ctx.RenderComponent<RadzenNumericRangeValidatorTestDouble>();

component.SetParametersAndRender(parameters =>
{
component.SetParametersAndRender(parameters => parameters.Add(p => p.Min, 0).Add(p => p.Max, 10).Add(p => p.AllowNull, true));
});

Assert.True(component.Instance.Validate(null));
}

[Fact]
public void Returns_False_If_Value_Overflows()
Expand Down
10 changes: 8 additions & 2 deletions Radzen.Blazor/RadzenNumericRangeValidator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ public class RadzenNumericRangeValidator : ValidatorBase
[Parameter]
public IComparable Max { get; set; }

/// <summary>
/// Specifies if value can be null. If true, a null component value will be accepted.
/// </summary>
[Parameter]
public bool AllowNull { get; set; } = false;

/// <inheritdoc />
protected override bool Validate(IRadzenFormComponent component)
{
Expand All @@ -54,7 +60,7 @@ protected override bool Validate(IRadzenFormComponent component)

if (value == null)
{
return false;
return AllowNull;
}


Expand Down Expand Up @@ -91,4 +97,4 @@ private bool TryConvertToType(object value, Type type, out object convertedValue
}
}
}
}
}

0 comments on commit 3561062

Please sign in to comment.