diff --git a/src/Components/Forms/src/PublicAPI.Unshipped.txt b/src/Components/Forms/src/PublicAPI.Unshipped.txt index 665338094430..3e45c55a93df 100644 --- a/src/Components/Forms/src/PublicAPI.Unshipped.txt +++ b/src/Components/Forms/src/PublicAPI.Unshipped.txt @@ -1,4 +1,8 @@ #nullable enable +*REMOVED*Microsoft.AspNetCore.Components.Forms.ValidationMessageStore.Add(System.Linq.Expressions.Expression!>! accessor, string! message) -> void +*REMOVED*Microsoft.AspNetCore.Components.Forms.ValidationMessageStore.Add(System.Linq.Expressions.Expression!>! accessor, System.Collections.Generic.IEnumerable! messages) -> void +Microsoft.AspNetCore.Components.Forms.ValidationMessageStore.Add(System.Linq.Expressions.Expression!>! accessor, string! message) -> void +Microsoft.AspNetCore.Components.Forms.ValidationMessageStore.Add(System.Linq.Expressions.Expression!>! accessor, System.Collections.Generic.IEnumerable! messages) -> void Microsoft.AspNetCore.Components.Forms.ValidationRequestedEventArgs.AddAsyncValidator(System.Func! validator) -> void *REMOVED*static Microsoft.AspNetCore.Components.Forms.EditContextDataAnnotationsExtensions.AddDataAnnotationsValidation(this Microsoft.AspNetCore.Components.Forms.EditContext! editContext) -> Microsoft.AspNetCore.Components.Forms.EditContext! *REMOVED*static Microsoft.AspNetCore.Components.Forms.EditContextDataAnnotationsExtensions.EnableDataAnnotationsValidation(this Microsoft.AspNetCore.Components.Forms.EditContext! editContext) -> System.IDisposable! diff --git a/src/Components/Forms/src/ValidationMessageStore.cs b/src/Components/Forms/src/ValidationMessageStore.cs index 7fec1a18b654..9db1892e5acf 100644 --- a/src/Components/Forms/src/ValidationMessageStore.cs +++ b/src/Components/Forms/src/ValidationMessageStore.cs @@ -35,7 +35,7 @@ public void Add(in FieldIdentifier fieldIdentifier, string message) /// /// Identifies the field for which to add the message. /// The validation message. - public void Add(Expression> accessor, string message) + public void Add(Expression> accessor, string message) => Add(FieldIdentifier.Create(accessor), message); /// @@ -51,7 +51,7 @@ public void Add(in FieldIdentifier fieldIdentifier, IEnumerable messages /// /// Identifies the field for which to add the messages. /// The validation messages to be added. - public void Add(Expression> accessor, IEnumerable messages) + public void Add(Expression> accessor, IEnumerable messages) => Add(FieldIdentifier.Create(accessor), messages); /// diff --git a/src/Components/Forms/test/ValidationMessageStoreTest.cs b/src/Components/Forms/test/ValidationMessageStoreTest.cs index c47a79872d7e..ff3fd1b65bf5 100644 --- a/src/Components/Forms/test/ValidationMessageStoreTest.cs +++ b/src/Components/Forms/test/ValidationMessageStoreTest.cs @@ -89,4 +89,37 @@ public void CanClearMessagesForAllFields() Assert.Empty(messages[field1]); Assert.Empty(messages[field2]); } +#nullable enable + [Fact] + public void CanAddMessagesUsingExpressionWithNullableProperty() + { + var model = new TestModel(); + var editContext = new EditContext(model); + var messages = new ValidationMessageStore(editContext); + + messages.Add(() => model.Text, "This value is not valid"); + + var fieldIdentifier = FieldIdentifier.Create(() => model.Text); + Assert.Equal(new[] { "This value is not valid" }, messages[fieldIdentifier]); + } + + [Fact] + public void CanAddMultipleMessagesUsingExpressionWithNullableProperty() + { + var model = new TestModel(); + var editContext = new EditContext(model); + var messages = new ValidationMessageStore(editContext); + var validationMessages = new[] { "First error", "Second error" }; + + messages.Add(() => model.Text, validationMessages); + + var fieldIdentifier = FieldIdentifier.Create(() => model.Text); + Assert.Equal(validationMessages, messages[fieldIdentifier]); + } + + private class TestModel + { + public string? Text { get; set; } + } +#nullable disable }