From d880a775f7fbf45fd460f7402360cd0e33ee2f28 Mon Sep 17 00:00:00 2001 From: James Yeung Date: Thu, 18 May 2023 01:35:51 +0000 Subject: [PATCH 1/3] Add EditContext.IsValid(..) to determine if a field has any associated validation messages --- src/Components/Forms/src/EditContext.cs | 17 +++++++++++++++++ .../Forms/src/PublicAPI.Unshipped.txt | 2 ++ 2 files changed, 19 insertions(+) diff --git a/src/Components/Forms/src/EditContext.cs b/src/Components/Forms/src/EditContext.cs index 940781d1cadd..c8f0e1955dd1 100644 --- a/src/Components/Forms/src/EditContext.cs +++ b/src/Components/Forms/src/EditContext.cs @@ -195,6 +195,23 @@ public bool IsModified(in FieldIdentifier fieldIdentifier) public bool IsModified(Expression> accessor) => IsModified(FieldIdentifier.Create(accessor)); + /// + /// Determines whether the specified fields in this has no associated validation messages. + /// + /// True if the field has no associated validation messages after validation; otherwise false. + public bool IsValid(in FieldIdentifier fieldIdentifier) + => _fieldStates.TryGetValue(fieldIdentifier, out var state) + ? state.GetValidationMessages().Any() + : false; + + /// + /// Determines whether the specified fields in this has no associated validation messages. + /// + /// Identifies the field whose current validation messages should be returned. + /// True if the field has no associated validation messages after validation; otherwise false. + public bool IsValid(Expression> accessor) + => IsValid(FieldIdentifier.Create(accessor)); + /// /// Validates this . /// diff --git a/src/Components/Forms/src/PublicAPI.Unshipped.txt b/src/Components/Forms/src/PublicAPI.Unshipped.txt index 20d22e152809..3f57b43db466 100644 --- a/src/Components/Forms/src/PublicAPI.Unshipped.txt +++ b/src/Components/Forms/src/PublicAPI.Unshipped.txt @@ -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!>! accessor) -> bool From 20cdbad5e231dd3e4305b15bd5f9554374e39e2f Mon Sep 17 00:00:00 2001 From: James Yeung Date: Sun, 13 Aug 2023 07:43:12 +0000 Subject: [PATCH 2/3] refactor and add tests --- src/Components/Forms/src/EditContext.cs | 4 +--- src/Components/Forms/test/EditContextTest.cs | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/src/Components/Forms/src/EditContext.cs b/src/Components/Forms/src/EditContext.cs index c8f0e1955dd1..06e3c4a74afc 100644 --- a/src/Components/Forms/src/EditContext.cs +++ b/src/Components/Forms/src/EditContext.cs @@ -200,9 +200,7 @@ public bool IsModified(Expression> accessor) /// /// True if the field has no associated validation messages after validation; otherwise false. public bool IsValid(in FieldIdentifier fieldIdentifier) - => _fieldStates.TryGetValue(fieldIdentifier, out var state) - ? state.GetValidationMessages().Any() - : false; + => GetValidationMessages(fieldIdentifier).Any(); /// /// Determines whether the specified fields in this has no associated validation messages. diff --git a/src/Components/Forms/test/EditContextTest.cs b/src/Components/Forms/test/EditContextTest.cs index 1c08859c6aba..c11700d14aa3 100644 --- a/src/Components/Forms/test/EditContextTest.cs +++ b/src/Components/Forms/test/EditContextTest.cs @@ -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() { From 8dda2fb7f858943f648f1f5a18a616db08f4dc8a Mon Sep 17 00:00:00 2001 From: James Yeung Date: Sun, 13 Aug 2023 10:20:53 +0000 Subject: [PATCH 3/3] fix judge --- src/Components/Forms/src/EditContext.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Components/Forms/src/EditContext.cs b/src/Components/Forms/src/EditContext.cs index 06e3c4a74afc..ce5df0948125 100644 --- a/src/Components/Forms/src/EditContext.cs +++ b/src/Components/Forms/src/EditContext.cs @@ -200,7 +200,7 @@ public bool IsModified(Expression> accessor) /// /// True if the field has no associated validation messages after validation; otherwise false. public bool IsValid(in FieldIdentifier fieldIdentifier) - => GetValidationMessages(fieldIdentifier).Any(); + => !GetValidationMessages(fieldIdentifier).Any(); /// /// Determines whether the specified fields in this has no associated validation messages.