From 266971dd6fa725f75a823d7300a155379599808b Mon Sep 17 00:00:00 2001 From: Barrett Lo <36118918+thebarrettlo@users.noreply.github.com> Date: Tue, 13 Feb 2024 11:42:01 -0800 Subject: [PATCH] Adds FieldIdentifier parameter to FluentValidationMessage (#1489) * Adds FieldIdentifier parameter to FluentValidationMessage * Adjusts code/comment style based on PR comments --- .../Forms/FluentValidationMessage.razor.cs | 25 ++++++++++++++----- 1 file changed, 19 insertions(+), 6 deletions(-) diff --git a/src/Core/Components/Forms/FluentValidationMessage.razor.cs b/src/Core/Components/Forms/FluentValidationMessage.razor.cs index 09b3fc37e6..7b48545c41 100644 --- a/src/Core/Components/Forms/FluentValidationMessage.razor.cs +++ b/src/Core/Components/Forms/FluentValidationMessage.razor.cs @@ -15,12 +15,21 @@ public partial class FluentValidationMessage : FluentComponentBase, IDis private readonly EventHandler? _validationStateChangedHandler; private FieldIdentifier _fieldIdentifier; - [CascadingParameter] private EditContext CurrentEditContext { get; set; } = default!; + [CascadingParameter] + private EditContext CurrentEditContext { get; set; } = default!; /// - /// Specifies the field for which validation messages should be displayed. + /// Gets or sets the for which validation messages should be displayed. + /// If set, this parameter takes precedence over . /// - [Parameter] public Expression>? For { get; set; } + [Parameter] + public FieldIdentifier? Field { get; set; } + + /// + /// Gets or sets the field for which validation messages should be displayed. + /// + [Parameter] + public Expression>? For { get; set; } /// protected string? ClassValue => new CssBuilder(Class) @@ -49,10 +58,14 @@ protected override void OnParametersSet() $"an {nameof(EditForm)}."); } - if (For == null) // Not possible except if you manually specify T + if (Field != null) + { + _fieldIdentifier = Field.Value; + } + else if (For == null) { - throw new InvalidOperationException($"{GetType()} requires a value for the " + - $"{nameof(For)} parameter."); + throw new InvalidOperationException($"{GetType()} requires a value for either " + + $"the {nameof(Field)} or {nameof(For)} parameter."); } else if (For != _previousFieldAccessor) {