Skip to content

Commit 29e9965

Browse files
author
Ryan Nowak
committed
Blazor API Review: Forms
Fixes: #12549
1 parent 5619385 commit 29e9965

19 files changed

+131
-137
lines changed

src/Components/Components/ref/Microsoft.AspNetCore.Components.netstandard2.0.cs

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -419,8 +419,10 @@ public event System.EventHandler<Microsoft.AspNetCore.Components.Forms.Validatio
419419
public Microsoft.AspNetCore.Components.Forms.FieldIdentifier Field(string fieldName) { throw null; }
420420
public System.Collections.Generic.IEnumerable<string> GetValidationMessages() { throw null; }
421421
public System.Collections.Generic.IEnumerable<string> GetValidationMessages(Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) { throw null; }
422+
public System.Collections.Generic.IEnumerable<string> GetValidationMessages(System.Linq.Expressions.Expression<System.Func<object>> accessor) { throw null; }
422423
public bool IsModified() { throw null; }
423424
public bool IsModified(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) { throw null; }
425+
public bool IsModified(System.Linq.Expressions.Expression<System.Func<object>> accessor) { throw null; }
424426
public void MarkAsUnmodified() { }
425427
public void MarkAsUnmodified(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) { }
426428
public void NotifyFieldChanged(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) { }
@@ -431,24 +433,20 @@ public static partial class EditContextDataAnnotationsExtensions
431433
{
432434
public static Microsoft.AspNetCore.Components.Forms.EditContext AddDataAnnotationsValidation(this Microsoft.AspNetCore.Components.Forms.EditContext editContext) { throw null; }
433435
}
434-
public static partial class EditContextExpressionExtensions
436+
public sealed partial class FieldChangedEventArgs : System.EventArgs
435437
{
436-
public static System.Collections.Generic.IEnumerable<string> GetValidationMessages(this Microsoft.AspNetCore.Components.Forms.EditContext editContext, System.Linq.Expressions.Expression<System.Func<object>> accessor) { throw null; }
437-
public static bool IsModified(this Microsoft.AspNetCore.Components.Forms.EditContext editContext, System.Linq.Expressions.Expression<System.Func<object>> accessor) { throw null; }
438-
}
439-
public sealed partial class FieldChangedEventArgs
440-
{
441-
internal FieldChangedEventArgs() { }
438+
public FieldChangedEventArgs(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) { }
442439
public Microsoft.AspNetCore.Components.Forms.FieldIdentifier FieldIdentifier { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
443440
}
444441
[System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential)]
445-
public readonly partial struct FieldIdentifier
442+
public readonly partial struct FieldIdentifier : System.IEquatable<Microsoft.AspNetCore.Components.Forms.FieldIdentifier>
446443
{
447444
private readonly object _dummy;
448445
public FieldIdentifier(object model, string fieldName) { throw null; }
449446
public string FieldName { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
450447
public object Model { [System.Runtime.CompilerServices.CompilerGeneratedAttribute]get { throw null; } }
451448
public static Microsoft.AspNetCore.Components.Forms.FieldIdentifier Create<T>(System.Linq.Expressions.Expression<System.Func<T>> accessor) { throw null; }
449+
public bool Equals(Microsoft.AspNetCore.Components.Forms.FieldIdentifier otherIdentifier) { throw null; }
452450
public override bool Equals(object obj) { throw null; }
453451
public override int GetHashCode() { throw null; }
454452
}
@@ -457,24 +455,23 @@ public sealed partial class ValidationMessageStore
457455
public ValidationMessageStore(Microsoft.AspNetCore.Components.Forms.EditContext editContext) { }
458456
public System.Collections.Generic.IEnumerable<string> this[Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier] { get { throw null; } }
459457
public System.Collections.Generic.IEnumerable<string> this[System.Linq.Expressions.Expression<System.Func<object>> accessor] { get { throw null; } }
458+
public void Add(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier, System.Collections.Generic.IEnumerable<string> messages) { }
460459
public void Add(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier, string message) { }
461-
public void AddRange(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier, System.Collections.Generic.IEnumerable<string> messages) { }
460+
public void Add(System.Linq.Expressions.Expression<System.Func<object>> accessor, System.Collections.Generic.IEnumerable<string> messages) { }
461+
public void Add(System.Linq.Expressions.Expression<System.Func<object>> accessor, string message) { }
462462
public void Clear() { }
463463
public void Clear(in Microsoft.AspNetCore.Components.Forms.FieldIdentifier fieldIdentifier) { }
464+
public void Clear(System.Linq.Expressions.Expression<System.Func<object>> accessor) { }
464465
}
465-
public static partial class ValidationMessageStoreExpressionExtensions
466-
{
467-
public static void Add(this Microsoft.AspNetCore.Components.Forms.ValidationMessageStore store, System.Linq.Expressions.Expression<System.Func<object>> accessor, string message) { }
468-
public static void AddRange(this Microsoft.AspNetCore.Components.Forms.ValidationMessageStore store, System.Linq.Expressions.Expression<System.Func<object>> accessor, System.Collections.Generic.IEnumerable<string> messages) { }
469-
public static void Clear(this Microsoft.AspNetCore.Components.Forms.ValidationMessageStore store, System.Linq.Expressions.Expression<System.Func<object>> accessor) { }
470-
}
471-
public sealed partial class ValidationRequestedEventArgs
466+
public sealed partial class ValidationRequestedEventArgs : System.EventArgs
472467
{
473-
internal ValidationRequestedEventArgs() { }
468+
public static readonly new Microsoft.AspNetCore.Components.Forms.ValidationRequestedEventArgs Empty;
469+
public ValidationRequestedEventArgs() { }
474470
}
475-
public sealed partial class ValidationStateChangedEventArgs
471+
public sealed partial class ValidationStateChangedEventArgs : System.EventArgs
476472
{
477-
internal ValidationStateChangedEventArgs() { }
473+
public static readonly new Microsoft.AspNetCore.Components.Forms.ValidationStateChangedEventArgs Empty;
474+
public ValidationStateChangedEventArgs() { }
478475
}
479476
}
480477
namespace Microsoft.AspNetCore.Components.Rendering

src/Components/Components/src/Forms/EditContext.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Linq;
7+
using System.Linq.Expressions;
78

89
namespace Microsoft.AspNetCore.Components.Forms
910
{
@@ -158,6 +159,16 @@ public IEnumerable<string> GetValidationMessages(FieldIdentifier fieldIdentifier
158159
}
159160
}
160161

162+
/// <summary>
163+
/// Gets the current validation messages for the specified field.
164+
///
165+
/// This method does not perform validation itself. It only returns messages determined by previous validation actions.
166+
/// </summary>
167+
/// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
168+
/// <returns>The current validation messages for the specified field.</returns>
169+
public IEnumerable<string> GetValidationMessages(Expression<Func<object>> accessor)
170+
=> GetValidationMessages(FieldIdentifier.Create(accessor));
171+
161172
/// <summary>
162173
/// Determines whether the specified fields in this <see cref="EditContext"/> has been modified.
163174
/// </summary>
@@ -167,6 +178,14 @@ public bool IsModified(in FieldIdentifier fieldIdentifier)
167178
? state.IsModified
168179
: false;
169180

181+
/// <summary>
182+
/// Determines whether the specified fields in this <see cref="EditContext"/> has been modified.
183+
/// </summary>
184+
/// <param name="accessor">Identifies the field whose current validation messages should be returned.</param>
185+
/// <returns>True if the field has been modified; otherwise false.</returns>
186+
public bool IsModified(Expression<Func<object>> accessor)
187+
=> IsModified(FieldIdentifier.Create(accessor));
188+
170189
/// <summary>
171190
/// Validates this <see cref="EditContext"/>.
172191
/// </summary>

src/Components/Components/src/Forms/EditContextDataAnnotationsExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ private static void ValidateField(EditContext editContext, ValidationMessageStor
7474

7575
Validator.TryValidateProperty(propertyValue, validationContext, results);
7676
messages.Clear(fieldIdentifier);
77-
messages.AddRange(fieldIdentifier, results.Select(result => result.ErrorMessage));
77+
messages.Add(fieldIdentifier, results.Select(result => result.ErrorMessage));
7878

7979
// We have to notify even if there were no messages before and are still no messages now,
8080
// because the "state" that changed might be the completion of some async validation task

src/Components/Components/src/Forms/EditContextExpressionExtensions.cs

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,27 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
46
namespace Microsoft.AspNetCore.Components.Forms
57
{
68
/// <summary>
79
/// Provides information about the <see cref="EditContext.OnFieldChanged"/> event.
810
/// </summary>
9-
public sealed class FieldChangedEventArgs
11+
public sealed class FieldChangedEventArgs : EventArgs
1012
{
1113
/// <summary>
12-
/// Identifies the field whose value has changed.
14+
/// Creates a new instance of <see cref="FieldChangedEventArgs"/>.
1315
/// </summary>
14-
public FieldIdentifier FieldIdentifier { get; }
15-
16-
internal FieldChangedEventArgs(in FieldIdentifier fieldIdentifier)
16+
/// <param name="fieldIdentifier">The <see cref="Forms.FieldIdentifier"/></param>
17+
public FieldChangedEventArgs(in FieldIdentifier fieldIdentifier)
1718
{
1819
FieldIdentifier = fieldIdentifier;
1920
}
21+
22+
/// <summary>
23+
/// Identifies the field whose value has changed.
24+
/// </summary>
25+
public FieldIdentifier FieldIdentifier { get; }
2026
}
2127
}

src/Components/Components/src/Forms/FieldIdentifier.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.AspNetCore.Components.Forms
1010
/// Uniquely identifies a single field that can be edited. This may correspond to a property on a
1111
/// model object, or can be any other named value.
1212
/// </summary>
13-
public readonly struct FieldIdentifier
13+
public readonly struct FieldIdentifier : IEquatable<FieldIdentifier>
1414
{
1515
/// <summary>
1616
/// Initializes a new instance of the <see cref="FieldIdentifier"/> structure.
@@ -68,8 +68,15 @@ public override int GetHashCode()
6868
/// <inheritdoc />
6969
public override bool Equals(object obj)
7070
=> obj is FieldIdentifier otherIdentifier
71-
&& otherIdentifier.Model == Model
72-
&& string.Equals(otherIdentifier.FieldName, FieldName, StringComparison.Ordinal);
71+
&& Equals(otherIdentifier);
72+
73+
/// <inheritdoc />
74+
public bool Equals(FieldIdentifier otherIdentifier)
75+
{
76+
return
77+
otherIdentifier.Model == Model &&
78+
string.Equals(otherIdentifier.FieldName, FieldName, StringComparison.Ordinal);
79+
}
7380

7481
private static void ParseAccessor<T>(Expression<Func<T>> accessor, out object model, out string fieldName)
7582
{

src/Components/Components/src/Forms/ValidationMessageStore.cs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,14 +33,30 @@ public ValidationMessageStore(EditContext editContext)
3333
public void Add(in FieldIdentifier fieldIdentifier, string message)
3434
=> GetOrCreateMessagesListForField(fieldIdentifier).Add(message);
3535

36+
/// <summary>
37+
/// Adds a validation message for the specified field.
38+
/// </summary>
39+
/// <param name="accessor">Identifies the field for which to add the message.</param>
40+
/// <param name="message">The validation message.</param>
41+
public void Add(Expression<Func<object>> accessor, string message)
42+
=> Add(FieldIdentifier.Create(accessor), message);
43+
3644
/// <summary>
3745
/// Adds the messages from the specified collection for the specified field.
3846
/// </summary>
3947
/// <param name="fieldIdentifier">The identifier for the field.</param>
4048
/// <param name="messages">The validation messages to be added.</param>
41-
public void AddRange(in FieldIdentifier fieldIdentifier, IEnumerable<string> messages)
49+
public void Add(in FieldIdentifier fieldIdentifier, IEnumerable<string> messages)
4250
=> GetOrCreateMessagesListForField(fieldIdentifier).AddRange(messages);
4351

52+
/// <summary>
53+
/// Adds the messages from the specified collection for the specified field.
54+
/// </summary>
55+
/// <param name="accessor">Identifies the field for which to add the messages.</param>
56+
/// <param name="messages">The validation messages to be added.</param>
57+
public void Add(Expression<Func<object>> accessor, IEnumerable<string> messages)
58+
=> Add(FieldIdentifier.Create(accessor), messages);
59+
4460
/// <summary>
4561
/// Gets the validation messages within this <see cref="ValidationMessageStore"/> for the specified field.
4662
///
@@ -74,6 +90,13 @@ public void Clear()
7490
_messages.Clear();
7591
}
7692

93+
/// <summary>
94+
/// Removes all messages within this <see cref="ValidationMessageStore"/> for the specified field.
95+
/// </summary>
96+
/// <param name="accessor">Identifies the field for which to remove the messages.</param>
97+
public void Clear(Expression<Func<object>> accessor)
98+
=> Clear(FieldIdentifier.Create(accessor));
99+
77100
/// <summary>
78101
/// Removes all messages within this <see cref="ValidationMessageStore"/> for the specified field.
79102
/// </summary>

src/Components/Components/src/Forms/ValidationMessageStoreExpressionExtensions.cs

Lines changed: 0 additions & 41 deletions
This file was deleted.

src/Components/Components/src/Forms/ValidationRequestedEventArgs.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
46
namespace Microsoft.AspNetCore.Components.Forms
57
{
68
/// <summary>
79
/// Provides information about the <see cref="EditContext.OnValidationRequested"/> event.
810
/// </summary>
9-
public sealed class ValidationRequestedEventArgs
11+
public sealed class ValidationRequestedEventArgs : EventArgs
1012
{
11-
internal static readonly ValidationRequestedEventArgs Empty = new ValidationRequestedEventArgs();
13+
/// <summary>
14+
/// Gets a shared empty instance of <see cref="ValidationRequestedEventArgs"/>.
15+
/// </summary>
16+
public static new readonly ValidationRequestedEventArgs Empty = new ValidationRequestedEventArgs();
1217

13-
internal ValidationRequestedEventArgs()
18+
/// <summary>
19+
/// Creates a new instance of <see cref="ValidationRequestedEventArgs"/>.
20+
/// </summary>
21+
public ValidationRequestedEventArgs()
1422
{
1523
}
1624
}

src/Components/Components/src/Forms/ValidationStateChangedEventArgs.cs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

4+
using System;
5+
46
namespace Microsoft.AspNetCore.Components.Forms
57
{
68
/// <summary>
79
/// Provides information about the <see cref="EditContext.OnValidationStateChanged"/> event.
810
/// </summary>
9-
public sealed class ValidationStateChangedEventArgs
11+
public sealed class ValidationStateChangedEventArgs : EventArgs
1012
{
11-
internal static readonly ValidationStateChangedEventArgs Empty = new ValidationStateChangedEventArgs();
13+
/// <summary>
14+
/// Gets a shared empty instance of <see cref="ValidationStateChangedEventArgs"/>.
15+
/// </summary>
16+
public new static readonly ValidationStateChangedEventArgs Empty = new ValidationStateChangedEventArgs();
1217

13-
internal ValidationStateChangedEventArgs()
18+
/// <summary>
19+
/// Creates a new instance of <see cref="ValidationStateChangedEventArgs" />
20+
/// </summary>
21+
public ValidationStateChangedEventArgs()
1422
{
1523
}
1624
}

0 commit comments

Comments
 (0)