Skip to content

Commit 27089dd

Browse files
authored
Add EditContext.IsValid(..) to determine if a field has any associated validation messages (#48698)
* Add EditContext.IsValid(..) to determine if a field has any associated validation messages * refactor and add tests * fix judge
1 parent 4802de1 commit 27089dd

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

src/Components/Forms/src/EditContext.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,21 @@ public bool IsModified(in FieldIdentifier fieldIdentifier)
195195
public bool IsModified(Expression<Func<object>> accessor)
196196
=> IsModified(FieldIdentifier.Create(accessor));
197197

198+
/// <summary>
199+
/// Determines whether the specified fields in this <see cref="EditContext"/> has no associated validation messages.
200+
/// </summary>
201+
/// <returns>True if the field has no associated validation messages after validation; otherwise false.</returns>
202+
public bool IsValid(in FieldIdentifier fieldIdentifier)
203+
=> !GetValidationMessages(fieldIdentifier).Any();
204+
205+
/// <summary>
206+
/// Determines whether the specified fields in this <see cref="EditContext"/> has no associated validation messages.
207+
/// </summary>
208+
/// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
209+
/// <returns>True if the field has no associated validation messages after validation; otherwise false.</returns>
210+
public bool IsValid(Expression<Func<object>> accessor)
211+
=> IsValid(FieldIdentifier.Create(accessor));
212+
198213
/// <summary>
199214
/// Validates this <see cref="EditContext"/>.
200215
/// </summary>
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#nullable enable
22
Microsoft.AspNetCore.Components.Forms.EditContext.ShouldUseFieldIdentifiers.get -> bool
33
Microsoft.AspNetCore.Components.Forms.EditContext.ShouldUseFieldIdentifiers.set -> void
4+
Microsoft.AspNetCore.Components.Forms.EditContext.IsValid(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) -> bool
5+
Microsoft.AspNetCore.Components.Forms.EditContext.IsValid(System.Linq.Expressions.Expression<System.Func<object!>!>! accessor) -> bool

src/Components/Forms/test/EditContextTest.cs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,24 @@ public void RequestsValidationWhenValidateIsCalled()
233233
Assert.Equal(new[] { "Some message" }, editContext.GetValidationMessages());
234234
}
235235

236+
[Fact]
237+
public void IsInvalidWithValidationMessagesForSpecifiedField()
238+
{
239+
// Arrange
240+
var editContext = new EditContext(new object());
241+
var messages = new ValidationMessageStore(editContext);
242+
var fieldOnThisModel1 = editContext.Field("field1");
243+
var fieldOnThisModel2 = editContext.Field("field2");
244+
messages.Add(
245+
fieldOnThisModel1,
246+
"Some message");
247+
248+
// Assert
249+
Assert.False(editContext.Validate());
250+
Assert.False(editContext.IsValid(fieldOnThisModel1));
251+
Assert.True(editContext.IsValid(fieldOnThisModel2));
252+
}
253+
236254
[Fact]
237255
public void LookingUpModel_ThatOverridesGetHashCodeAndEquals_Works()
238256
{

0 commit comments

Comments
 (0)