I'm strugling to implement FluentValidations while inline editing DataGrid... Inline editing works fine as does validations... but i never see validation messages, if it's not valid i just can't (correctly) save.
private async Task InlineEditEnter(T model)
{
inlineEditContext = new(model);
inlineEditContextMessageStore = new(inlineEditContext);
Type typeVal = typeof(IValidator<>).MakeGenericType(typeof(T));
IValidator? validator = serviceProvider.GetService(typeVal) as IValidator;
if(validator is not null)
inlineEditContext.OnValidationRequested += async (sender, args) => {
inlineEditContextMessageStore.Clear();
ValidationResult validationResult = await validator.ValidateAsync(new ValidationContext<T>(model));
foreach(ValidationFailure error in validationResult.Errors)
{
FieldIdentifier field = new FieldIdentifier(model, error.PropertyName);
inlineEditContextMessageStore.Add(field, error.ErrorMessage);
}
inlineEditContext.NotifyValidationStateChanged();
};
await grid!.EditRow(model);
}
private void InlineEditCancel(T model) =>
grid!.CancelEditRow(model);
private async Task InlineEditSave(T model)
{
await inlineEditContext!.ValidateAsync();
if(inlineEditContext!.GetValidationMessages().Any())
return;
if(InlineSaveAction is not null)
await InlineSaveAction(model);
await grid!.UpdateRow(model);
}
I can enter the inline edit mode, edit values, if it's valid i can save and the changes are persisted. But if the model state is not valid according to FluentValidations validator, then i can't save, which is good, but the validation messages are not shown. I think my dynamic EditContext is to blame... I know that Radzen's example is using custom Radzen validator components, but is there any way to implement it with standard components like ValidationMessage?
@enchev Well… It sort of works… Problem is the <FluentValidator /> (and i presume <FluentValidationValidator />) are working with EditContext somehow. And in inline edit mode, there are many edit contexts (for each model element)? Because for this setup:
When there are 2 validation errors one for Name and one for NameEN they are both displayed under Name and none under NameEN. If the second <FluentValidator AsyncMode /> is removed, only one error is displayed under Name. If the first <FluentValidator AsyncMode /> is removed, again one error also under Name curiously.
So I made it work with the intensive Claude probing ... I'll post it here, maybe someone will find it useful.
I have a grid component, that wraps RadzenDataGrid and is doing some other grid related stuff. So majority of these changes are made there.
You'll need another UI-less component GridCaptureInlineEditContext, that will capture EditContext which is Radzen cascading to the inline edited values:
When you use wrapped grid component the thing that makes edit inline is that you hook InlineSave function and specify columns EditTemplate, else standard page / dialog is used.
<Grid
@ref="@grid"
T="T"
InlineSave="@OnSave"
>
<MyColumns>
<RadzenDataGridColumn TItem="T" Property="Name" Title="Name">
<EditTemplate Context="x">
<RadzenTextBox @bind-Value="@x.Name" />
<ValidationMessage For="() => x.Name" />
</EditTemplate>
</RadzenDataGridColumn>
<RadzenDataGridColumn TItem="T" Property="NameEN" Title="Name (EN)">
<EditTemplate Context="x">
<RadzenTextBox @bind-Value="@x.NameEN" />
<ValidationMessage For="() => x.NameEN" />
</EditTemplate>
</RadzenDataGridColumn>
</MyColumns>
</Seznam>
@code {
...
private async Task OnSave(T row, bool isInsert)
{
// your local model or db save...
}
// you'll hook up this method to some button
public Task OnInsert(T model) =>
grid?.InlineEditEnterInsertAsync(model) ?? Task.CompletedTask;
}