Skip to content

Commit

Permalink
Adds FieldIdentifier parameter to FluentValidationMessage (#1489)
Browse files Browse the repository at this point in the history
* Adds FieldIdentifier parameter to FluentValidationMessage

* Adjusts code/comment style based on PR comments
  • Loading branch information
thebarrettlo authored Feb 13, 2024
1 parent 144c17e commit 266971d
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions src/Core/Components/Forms/FluentValidationMessage.razor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,21 @@ public partial class FluentValidationMessage<TValue> : FluentComponentBase, IDis
private readonly EventHandler<ValidationStateChangedEventArgs>? _validationStateChangedHandler;
private FieldIdentifier _fieldIdentifier;

[CascadingParameter] private EditContext CurrentEditContext { get; set; } = default!;
[CascadingParameter]
private EditContext CurrentEditContext { get; set; } = default!;

/// <summary>
/// Specifies the field for which validation messages should be displayed.
/// Gets or sets the <see cref="FieldIdentifier"/> for which validation messages should be displayed.
/// If set, this parameter takes precedence over <see cref="For"/>.
/// </summary>
[Parameter] public Expression<Func<TValue>>? For { get; set; }
[Parameter]
public FieldIdentifier? Field { get; set; }

/// <summary>
/// Gets or sets the field for which validation messages should be displayed.
/// </summary>
[Parameter]
public Expression<Func<TValue>>? For { get; set; }

/// <summary />
protected string? ClassValue => new CssBuilder(Class)
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 266971d

Please sign in to comment.