I looked at the following solution, but NuGet package Microsoft.AspNetCore.Components.DataAnnotations.Validation is deprecated.
Blazor Complex Validation between two nested Objects
Thanks!
I looked at the following solution, but NuGet package Microsoft.AspNetCore.Components.DataAnnotations.Validation is deprecated.
Blazor Complex Validation between two nested Objects
Thanks!
Easiest I can think of is using a different edit context for the nested objects, then propagating the eventual error state to the main edit context (the one cascading from the form).
This behaviour can be wrapped in a component, I call it EditScope
@using Microsoft.AspNetCore.Components.Forms
@implements IDisposable
<CascadingValue Value="EditContext" IsFixed="true">
<DataAnnotationsValidator />
@ChildContent
</CascadingValue>
@code {
[CascadingParameter]
public required EditContext ParentEditContext { get; set; }
[Parameter]
public required RenderFragment ChildContent { get; set; }
[Parameter]
public required object Model { get; set; }
[Parameter] public bool Validate { get; set; } = true;
private EditContext? _editContext;
private ValidationMessageStore? _messageStore;
private ValidationMessageStore? _parentMessageStore;
public EditContext EditContext => _editContext ??= new EditContext(Model);
protected override void OnInitialized() {
base.OnInitialized();
ParentEditContext.OnValidationRequested += ParentEditContextOnOnValidationRequested;
_messageStore = new ValidationMessageStore(EditContext);
_parentMessageStore = new ValidationMessageStore(ParentEditContext);
}
private void ParentEditContextOnOnValidationRequested(object? sender, ValidationRequestedEventArgs e) {
_messageStore?.Clear();
_parentMessageStore?.Clear();
if (!Validate) {
return;
}
if (!EditContext.Validate()) {
_parentMessageStore?.Add(() => Model, "Invalid");
}
}
public void Dispose() {
ParentEditContext.OnValidationRequested -= ParentEditContextOnOnValidationRequested;
}
}
<EditForm Model="InputModel"OnValidSubmit="SaveAsync">
<InputText @bind-Value="InputModel.Name"/>
<ValidationMessage For="() => InputModel.Name"/>
<EditScope Model="InputModel.NestedModel">
<InputText @bind-Value="InputModel.NestedModel.Name"/>
<ValidationMessage For="() => InputModel.NestedModel.Name"/>
</EditScope>
</EditForm>
Nesting scopes should work too, but I've never tried
<ValidationMessage For="..." /> is usedfalse value for the Validate parameter (I often have an IsDeleted property on my input models, so I use Validate='!model.IsDeleted')<ValidationSummmary /> (I almost never use it)