Skip to content

Commit

Permalink
MudForm: Only set Validation if For is set (#5419)
Browse files Browse the repository at this point in the history
MudForm: Only set Validation if For is not null
  • Loading branch information
Mr-Technician authored Oct 10, 2022
1 parent 4fcb43d commit 83b6bcb
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

<PropertyGroup Condition=" '$(RunConfiguration)' == 'MudBlazor.UnitTests.Viewer' " />
<ItemGroup>
<PackageReference Include="FluentValidation" Version="11.2.2" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly" Version="6.0.9.*" />
<PackageReference Include="Microsoft.AspNetCore.Components.WebAssembly.DevServer" Version="6.0.9.*" PrivateAssets="all" />
<PackageReference Include="System.Net.Http.Json" Version="6.0.0.*" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
@namespace MudBlazor.UnitTests.TestComponents
@using FluentValidation

<MudForm Model="@Model" Validation="@(ValidationRules.ValidateValue)">
<MudTextField @bind-Value="Model.String1" For="(() => Model.String1)" />
<MudTextField @bind-Value="Model.String2" />
<MudDatePicker @bind-Date="Model.DateTime1" For="(() => Model.DateTime1)" />
<MudDatePicker @bind-Date="Model.DateTime2" />
</MudForm>

@code {
TestClass Model = new();
TestClassValidator ValidationRules = new();

public class TestClass
{
public string String1 { get; set; }
public string String2 { get; set; }
public DateTime? DateTime1 { get; set; }
public DateTime? DateTime2 { get; set; }
}

public class TestClassValidator : AbstractValidator<TestClass>
{
public Func<object, string, Task<IEnumerable<string>>> ValidateValue => async (model, propertyName) =>
{
var result = await ValidateAsync(ValidationContext<TestClass>.CreateWithOptions((TestClass)model, x => x.IncludeProperties(propertyName)));
if (result.IsValid)
return Array.Empty<string>();
return result.Errors.Select(e => e.ErrorMessage);
};
}
}
22 changes: 22 additions & 0 deletions src/MudBlazor.UnitTests/Components/FormTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1230,5 +1230,27 @@ public async Task FieldChangedEventShouldTrigger()
comp.Instance.FormFieldChangedEventArgs.NewValue.Should().Be(1);
comp.Instance.FormFieldChangedEventArgs.Field.Equals(numeric);
}

/// <summary>
/// When Validation is set on a Form, it should only set validation on fields that have a For parameter
/// </summary>
[Test]
public async Task FormAutoValidationSetTest()
{
var comp = Context.RenderComponent<FormAutomaticValidationTest>();
var textComps = comp.FindComponents<MudTextField<string>>();
var dateComps = comp.FindComponents<MudDatePicker>();

textComps[0].Instance.For.Should().NotBeNull(); //For is set
textComps[1].Instance.For.Should().BeNull(); //For is not set
dateComps[0].Instance.For.Should().NotBeNull(); //For is set
dateComps[1].Instance.For.Should().BeNull(); //For is not set

//Ensure Validation is only set where For is set
textComps[0].Instance.Validation.Should().NotBeNull(); //Validation is set
textComps[1].Instance.Validation.Should().BeNull(); //Validation is not set
dateComps[0].Instance.Validation.Should().NotBeNull(); //Validation is set
dateComps[1].Instance.Validation.Should().BeNull(); //Validation is not set
}
}
}
2 changes: 2 additions & 0 deletions src/MudBlazor/Base/MudFormComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ internal void EditFormValidate()
public Expression<Func<T>>? For { get; set; }
#nullable disable

public bool IsForNull => For == null;

/// <summary>
/// Stores the list of validation attributes attached to the property targeted by <seealso cref="For"/>. If <seealso cref="For"/> is null, this property is null too.
/// </summary>
Expand Down
2 changes: 1 addition & 1 deletion src/MudBlazor/Components/Form/MudForm.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ private void SetDefaultControlValidation(object validation, bool overrideFieldVa

foreach (var formControl in _formControls)
{
if (formControl.Validation == null || overrideFieldValidation)
if (!formControl.IsForNull && (formControl.Validation == null || overrideFieldValidation))
{
formControl.Validation = validation;
}
Expand Down
1 change: 1 addition & 0 deletions src/MudBlazor/Interfaces/IFormComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ public interface IFormComponent
public bool HasErrors { get; }
public bool Touched { get; }
public object Validation { get; set; }
public bool IsForNull { get; }
public List<string> ValidationErrors { get; set; }
public Task Validate();
public void Reset();
Expand Down

0 comments on commit 83b6bcb

Please sign in to comment.