Skip to content

Add EditContext.IsValid(..) to determine if a field has any associated validation messages #48698

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

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/Components/Forms/src/EditContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,21 @@ public bool IsModified(in FieldIdentifier fieldIdentifier)
public bool IsModified(Expression<Func<object>> accessor)
=> IsModified(FieldIdentifier.Create(accessor));

/// <summary>
/// Determines whether the specified fields in this <see cref="EditContext"/> has no associated validation messages.
/// </summary>
/// <returns>True if the field has no associated validation messages after validation; otherwise false.</returns>
public bool IsValid(in FieldIdentifier fieldIdentifier)
=> !GetValidationMessages(fieldIdentifier).Any();

/// <summary>
/// Determines whether the specified fields in this <see cref="EditContext"/> has no associated validation messages.
/// </summary>
/// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
/// <returns>True if the field has no associated validation messages after validation; otherwise false.</returns>
public bool IsValid(Expression<Func<object>> accessor)
=> IsValid(FieldIdentifier.Create(accessor));

/// <summary>
/// Validates this <see cref="EditContext"/>.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions src/Components/Forms/src/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#nullable enable
Microsoft.AspNetCore.Components.Forms.EditContext.ShouldUseFieldIdentifiers.get -> bool
Microsoft.AspNetCore.Components.Forms.EditContext.ShouldUseFieldIdentifiers.set -> void
Microsoft.AspNetCore.Components.Forms.EditContext.IsValid(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) -> bool
Microsoft.AspNetCore.Components.Forms.EditContext.IsValid(System.Linq.Expressions.Expression<System.Func<object!>!>! accessor) -> bool
18 changes: 18 additions & 0 deletions src/Components/Forms/test/EditContextTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,24 @@ public void RequestsValidationWhenValidateIsCalled()
Assert.Equal(new[] { "Some message" }, editContext.GetValidationMessages());
}

[Fact]
public void IsInvalidWithValidationMessagesForSpecifiedField()
{
// Arrange
var editContext = new EditContext(new object());
var messages = new ValidationMessageStore(editContext);
var fieldOnThisModel1 = editContext.Field("field1");
var fieldOnThisModel2 = editContext.Field("field2");
messages.Add(
fieldOnThisModel1,
"Some message");

// Assert
Assert.False(editContext.Validate());
Assert.False(editContext.IsValid(fieldOnThisModel1));
Assert.True(editContext.IsValid(fieldOnThisModel2));
}

[Fact]
public void LookingUpModel_ThatOverridesGetHashCodeAndEquals_Works()
{
Expand Down